如何在wcf自托管时发布wcf服务元数据?

时间:2014-04-17 16:00:43

标签: c# .net web-services wcf self-hosting

我用谷歌搜索了几天,几乎尝试了一切,但我仍然无法使这些东西工作。

我有2个WCF服务。我使用自托管,而不是IIS(由于某些原因,IIS不适合我)。一个是双工,另一个是标准。这是他们的合同: 双工:

[ServiceContract(SessionMode = SessionMode.Required, CallbackContract = typeof(IServiceCallback))]
public interface IClientService
{
    [OperationContract(IsOneWay = true)]
    void SolveTask(string pipelineName, string data);

    [OperationContract(IsOneWay = true)]
    void GenerateTask(List<GeneratorMethod> parameters);

    [OperationContract]
    bool Ping();
}

public interface IServiceCallback
{

    [OperationContract(IsOneWay = true)]
    void SendResult(SampleAnswer[] answers);

    [OperationContract(IsOneWay = true)]
    void RequestGeneratorParameters();

    [OperationContract(IsOneWay = true)]
    void SendGenerationResult(string text);
}

经典:

[ServiceContract]    
public interface IServerManagementService
{
    [OperationContract]
    [FaultContract(typeof(XmlError))]
    [FaultContract(typeof(UnknownError))]
    [FaultContract(typeof(InitializationError))]
    void InitializeServer();

    [OperationContract]
    void StartServer();

    [OperationContract]
    void StopServer();

    [OperationContract]
    void RestartServer();
}

我有以下配置:

<configuration>

                                                                                                                                 

<services>

  <service behaviorConfiguration="Service" name="LinProgWebServer.ClientService">
    <endpoint address="net.tcp://localhost:8078/LinProgWebServer/ClientService"
      binding="netTcpBinding" bindingConfiguration="netTcpEventBinding"
      contract="LinProgWebServer.IClientService">
      <identity>
        <dns value="localhost" />
      </identity>
    </endpoint>
    <endpoint address="mex" binding="mexHttpBinding" bindingConfiguration=""
      contract="IMetadataExchange" />
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:8731/Design_Time_Addresses/LinProgWebServer/ClientService" />
      </baseAddresses>
    </host>
  </service>
  <!--сервис управления сервером-->
  <!--<service behaviorConfiguration="Service" name="LinProgWebServer.ServerManagementService">
    <endpoint address="net.tcp://localhost:8079/LinProgWebServer/ManagementService"
      binding="netTcpBinding" bindingConfiguration="netTcpEventBinding"
      contract="LinProgWebServer.IServerManagementService">
      <identity>
        <dns value="localhost" />
      </identity>
    </endpoint>
    <endpoint address="mex" binding="mexHttpBinding" bindingConfiguration=""
      contract="IMetadataExchange" />
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:8732/Design_Time_Addresses/LinProgWebServer/ManagementService" />
      </baseAddresses>
    </host>
  </service>-->

</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="Service">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="false" />
    </behavior>
    <behavior name="">
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="false" />
    </behavior>
  </serviceBehaviors>
</behaviors>

现在我遇到了大麻烦:我可以通过添加服务参考找到经典服务,但无法找到双工服务。我试过netstat,它说两个服务都在监听他们的端口。我做错了什么?

这是我得到的例外:

    There was an error downloading   'http://localhost:8731/Design_Time_Addresses/LinProgWebServer/ClientService/_vti_bin/ListData.svc/$metadata'.
The request failed with HTTP status 405: Method Not Allowed.
Metadata contains a reference that cannot be resolved: 'http://localhost:8731/Design_Time_Addresses/LinProgWebServer/ClientService'.
There was no endpoint listening at http://localhost:8731/Design_Time_Addresses/LinProgWebServer/ClientService that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.
The remote server returned an error: (404) Not Found.
If the service is defined in the current solution, try building the solution and adding the service reference again.

我会感激任何帮助。

4 个答案:

答案 0 :(得分:0)

确保.svc文件具有您在服务参考

中添加的类的正确名称

如果您添加了名为“Foo”的服务,它应该如下所示:

<%@ ServiceHost Language="C#" Debug="true" Service="Foo" CodeBehind="Foo.svc.cs" %

另外,请确保你有      在你的web.config

答案 1 :(得分:0)

我不确定这是否有用。我所有的配置和服务都很好,但我有类型的雾,涉及wcf服务的工作,我还没有标记为datacontract类型之一。你很惊讶为什么服务开始时没有错误,并向我展示了这些没有信息的信息。另外,感谢Yuval提醒InstanceContextMode属性。

答案 2 :(得分:0)

将此添加到您设置端点的区域并引用您的服务类:

//config service metadata
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;


   ServiceMetadataBehavior mb = new ServiceMetadataBehavior();
   ServiceHost.Description.Behaviors.Add(mb);

   if (bUseSSL) {
        mb.HttpsGetEnabled = true;
        mb.HttpGetEnabled = false;
        ServiceHost.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName, MetadataExchangeBindings.CreateMexHttpsBinding(), "mex");
    } else {
        mb.HttpsGetEnabled = false;
        mb.HttpGetEnabled = true;
        ServiceHost.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName, MetadataExchangeBindings.CreateMexHttpBinding(), "mex");
    }

答案 3 :(得分:0)

您不应该使用与在开发环境中托管它相同的URL。无论如何,Localhost在机器外部没有任何意义。使用机器的IP地址以及 - 您可能必须使用netsh打开该机器上的端口。