Windows 应用程序中的 WCF elf 托管服务

WCF elf Hosted Service in Windows Application

我创建了一个示例桌面应用程序来检查 windows 应用程序中的自托管 REST 服务。这是我的样品

public class PayMentService : IPayMentService
{
    public string PayBill()
    {
        return "Transaction having PayId 12 was successful";
    }
}

[ServiceContract]
public interface IPayMentService
{
    [OperationContract]
    [WebInvoke(Method = "GET", UriTemplate = "/PayBill", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    string PayBill();

}

我的配置文件是这样的

<configuration>
  <system.serviceModel>
    <behaviors>
      <endpointBehaviors>
        <behavior name="JsonBehavior">
          <webHttp />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service name="SelfHostedWCFService.WCFService">
        <endpoint address="" behaviorConfiguration="JsonBehavior" binding="webHttpBinding"
          contract="SelfHostedWCFService.IWCFService" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8785" />
          </baseAddresses>
        </host>
      </service>
      <service name="SelfHostedWCFService.WCFCheck">
        <endpoint address="" binding="basicHttpBinding" contract="SelfHostedWCFService.IWCFCheck">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8733/Design_Time_Addresses/SelfHostedWCFService/WCFCheck/" />
          </baseAddresses>
        </host>
      </service>
      <service name="SelfHostedWCFService.PayMentService">
        <endpoint address="" binding="basicHttpBinding" contract="SelfHostedWCFService.IPayMentService">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8733/Design_Time_Addresses/SelfHostedWCFService/PayMentService/" />
          </baseAddresses>
        </host>
      </service>
    </services>
  </system.serviceModel>
</configuration>

并且我将其托管到配置好的 URL 中,例如

    static void Main()
    {
        ServiceHost host = new ServiceHost(typeof(SelfHostedWCFService.PayMentService));
        host.Open();
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }

我试图在 URL http://localhost:8785/service/PayMentService/PayBill 的帮助下调用我的服务。但它失败了。我知道服务已启动 运行,我可以在我的 PayMentService 构造函数中捕获我的请求,但我无法执行我的 PayBill() 函数。我尝试了不同的选择,但都没有 works.Can 有人给点建议..?

提前致谢

我试图在 URL http://localhost:8785/service/PayMentService/PayBill 的帮助下调用我的服务。但它失败了。 您需要启用已被 default.You 关闭的元数据发现,几乎没有,缺少的是

命名元服务行为实现。

 <serviceBehaviors>
        <behavior name="metadataDiscovery">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>

将其添加到您的支付服务中。

 <service name="SelfHostedWCFService.PayMentService" behaviorConfiguration="metadataDiscovery">
        <endpoint address="" binding="basicHttpBinding" contract="SelfHostedWCFService.IPayMentService">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />

现在可以浏览wsdl了,http://localhost:8733/Design_Time_Addresses/SelfHostedWCFService/PayMentService/。类似地,将行为标签配置添加到所有服务名称标签中。

您还解释了构造函数在调试模式下命中,您的代码中缺少一些东西。

 <endpointBehaviors>    
        <behavior name="JsonBehavior">   
          <webHttp/>    
        </behavior>    
      </endpointBehaviors>   

    <endpoint binding="webHttpBinding" 
contract="PayMentRESTService.IPayMentService" behaviorConfiguration="JsonBehavior">

详见http://www.c-sharpcorner.com/UploadFile/0c1bb2/creating-wcf-rest-service/