Unity WCF服务托管

时间:2015-10-12 11:03:42

标签: c# wcf unity3d

我正在尝试在Unity测试应用中自行托管WCF服务。

打开服务时没有报告错误,但我无法访问它或它的元数据(使用VS 2013提供的WcfTestClient进行测试)。

以下是我用于创建服务的代码:

_host = new ServiceHost(typeof(HomeAutomationService), new Uri("http://0.0.0.0:38001/"));
ServiceMetadataBehavior smb = _host.Description.Behaviors.Find<ServiceMetadataBehavior>() ??
                                new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
_host.Description.Behaviors.Add(smb);


_host.AddServiceEndpoint(typeof(IHomeAutomationService), new BasicHttpBinding(), "");
_host.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName,
    MetadataExchangeBindings.CreateMexHttpBinding(), "mex");

_host.Faulted += HostOnFaulted;

_host.Open();

我正在尝试使用以下Uri访问本地计算机上的元数据:http://localhost:38001/mex,但这只会返回HTTP400错误

1 个答案:

答案 0 :(得分:0)

您需要向服务添加元数据交换(mex)端点:

<services>
   <service name="MyService.MyService" behaviorConfiguration="metadataBehavior">
      <endpoint 
          address="http://localhost/MyService.svc" 
          binding="customBinding" bindingConfiguration="jsonpBinding" 
          behaviorConfiguration="MyService.MyService"
          contract="MyService.IMyService"/>
      <endpoint 
          address="mex" 
          binding="mexHttpBinding" 
          contract="IMetadataExchange"/>
   </service>
</services>
相关问题