在 WPF 应用程序上部署 WCF 服务时出错

Error in deploying WCF service on WPF application

我正在开发 WCF 应用程序,启动简单的 HelloWorld 服务。我开发了简单的 WPF 应用程序来托管服务,即启动和停止服务

当我尝试在 WCFTestClient 上使用 url“http://localhost:8087/MyServices/HelloWorldService”测试此服务时,出现以下错误

Error: Cannot obtain Metadata from http://localhost:8087/MyServices/HelloWorldService If this is a Windows (R) Communication Foundation service to which you have access, please check that you have enabled metadata publishing at the specified address.  For help enabling metadata publishing, please refer to the MSDN documentation at http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata Exchange Error    URI: http://localhost:8087/CreditUnionServices/HelloWorldService    Metadata contains a reference that cannot be resolved: 'http://localhost:8087/CreditUnionServices/HelloWorldService'.    <?xml version="1.0" encoding="utf-16"?><Fault xmlns="http://www.w3.org/2003/05/soap-envelope"><Code><Value>Sender</Value><Subcode><Value xmlns:a="http://schemas.xmlsoap.org/ws/2005/02/sc">a:BadContextToken</Value></Subcode></Code><Reason><Text xml:lang="en-GB">The message could not be processed. This is most likely because the action 'http://schemas.xmlsoap.org/ws/2004/09/transfer/Get' is incorrect or because the message contains an invalid or expired security context token or because there is a mismatch between bindings. The security context token would be invalid if the service aborted the channel due to inactivity. To prevent the service from aborting idle sessions prematurely increase the Receive timeout on the service endpoint's binding.</Text></Reason></Fault>HTTP GET Error    URI: http://localhost:8087/CreditUnionServices/HelloWorldService    There was an error downloading 'http://localhost:8087/CreditUnionServices/HelloWorldService'.    The request failed with HTTP status 400: Bad Request.

app.config

 <system.serviceModel>
<services>
  <service name="App.Services.Managers.HelloWorldManager">
    <endpoint address="HelloWorldService"
              binding="wsHttpBinding"
              contract="App.Services.Contracts.IHelloWorldService">
    </endpoint>
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:8087/MyService"/>
      </baseAddresses>
    </host>
  </service>
</services>

C#class开启关闭服务

public class ServicesHostManager
{
    ServiceHost _helloWorldServicesHost = new ServiceHost(typeof(HelloWorldManager));

    public void ProcessHelloWorldService(string _process)
    {
        if(!string.IsNullOrEmpty(_process))
        {
            try
            {
                if (_process.Equals("open_service"))
                {
                    _helloWorldServicesHost.Open();
                }
                else if (_process.Equals("close_service"))
                {
                    _helloWorldServicesHost.Close();
                }
            }
            catch (Exception e)
            {
                System.Windows.MessageBox.Show(e.ToString());
            }
        }
    }

前面的评论是对的,你需要声明一个mex端点。 mex 是提供元数据的东西。

尝试以下配置:

<system.serviceModel>
<services>
  <service name="App.Services.Managers.HelloWorldManager" behaviorConfiguration="SimpleWcfServiceBehavior">
    <endpoint address="HelloWorldService"
              binding="wsHttpBinding"
              contract="App.Services.Contracts.IHelloWorldService">
    </endpoint>
    <endpoint address="mex" binding="mexTcpBinding" bindingConfiguration="" contract="IMetadataExchange"></endpoint>
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:8087/MyService"/>
      </baseAddresses>
    </host>
  </service>
</services>

<behaviors>
  <serviceBehaviors>
    <behavior name="SimpleWcfServiceBehavior">
      <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
      <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
      <serviceDebug includeExceptionDetailInFaults="false"/>
    </behavior>
  </serviceBehaviors>
</behaviors>
</system.serviceModel>

我有更新代码并且正在工作,

 <system.serviceModel>
<services>
  <service name="App.Services.Managers.HelloWorldManager" behaviorConfiguration="DefaultServiceBehavior">
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:8087/MyService"/>
      </baseAddresses>
    </host>
    <endpoint address="HelloWorldService" binding="wsHttpBinding" contract="App.Services.Contracts.IHelloWorldService"></endpoint>
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="DefaultServiceBehavior">
      <serviceMetadata httpGetEnabled="True" httpsGetEnabled="true"/>
      <serviceDebug includeExceptionDetailInFaults="False" />
    </behavior>
  </serviceBehaviors>
</behaviors>