【问题标题】:WCF elf Hosted Service in Windows ApplicationWindows 应用程序中的 WCF elf 托管服务
【发布时间】:2017-10-29 10:41:51
【问题描述】:

我创建了一个示例桌面应用程序来检查 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() 函数。我尝试了不同的选择,但没有任何效果。谁能给点建议..?

提前致谢

【问题讨论】:

    标签: rest wcf windows-applications self-hosting


    【解决方案1】:

    我尝试通过 URL http://localhost:8785/service/PayMentService/PayBill 调用我的服务。但它失败了。 您需要启用默认关闭的元数据发现。您几乎在那里,缺少的一个是

    命名元服务行为实现。

     <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/

    【讨论】:

      猜你喜欢
      • 2011-10-05
      • 1970-01-01
      • 1970-01-01
      • 2017-10-16
      • 1970-01-01
      • 2012-11-29
      • 2017-01-20
      • 1970-01-01
      • 2011-01-07
      相关资源
      最近更新 更多