公开Metadata Exchange以与svcutil一起使用

时间:2012-08-28 20:40:58

标签: wcf service metadata svcutil.exe

我正在尝试使用一个非常简单(基本上为空/无功能)的服务,并使用svcutil.exe生成代理。这是我的服务器:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.ServiceModel;

class Server2
{
    static void Main(string[] args)
    {
        Console.WriteLine("Server");

        Uri baseAddress = new Uri("http://localhost:1234");
        var host = new ServiceHost(typeof(TheContractService), baseAddress);
        //host.AddServiceEndpoint(typeof(TheContractService), new WSHttpBinding(), "ContractService");
        host.Open();

        Console.ReadLine();
    }
}

[ServiceContract]
class TheContractService
{
    [OperationContract]
    void Expose()
    {
        Console.WriteLine("Exposed");
    }
}

[DataContract]
class TheContract
{
    [DataMember]
    public string PublicProperty { get; set; }
    [DataMember]
    public string PublicField;
    [DataMember]
    private string PrivateProperty { get; set; }
    [DataMember]
    private string PrivateField;
    [DataMember (Name = "BetterName")]
    private string fudshjguhf;
}

现在我只需要设置我的.config文件以允许MEX - 这是我的服务器配置:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>    
    <services>
      <service name="ContractService"
               behaviorConfiguration="MexServiceBehavior">

        <endpoint address="ContractService"
                  binding="basicHttpBinding"
                  contract="TheContractService"
        />

        <endpoint address="mex"
                  binding="mexHttpBinding"
                  contract="IMetadataExchange"
        />
      </service>
    </services>

    <behaviors>
      <serviceBehaviors>
        <behavior name="MexServiceBehavior">
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>                  
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

我在这里做错了什么?当我尝试运行此命令时: svcutil /t:code http://localhost:1234/mex /out ContractService.cs /config: ContractService.config

我收到400或405错误,并且未成功生成客户端代理。任何人都可以看到我目前有什么问题吗?谢谢!

1 个答案:

答案 0 :(得分:0)

您的班级名称为TheContractService,但在您的配置中,name元素的<service>属性为 ContractService 。确保该属性的值是完全限定名称(名称空间,如果有的话,加上类名),否则将不会选择配置。