我创建了一个示例桌面应用程序来检查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>
我将其托管到配置的网址,如
static void Main()
{
ServiceHost host = new ServiceHost(typeof(SelfHostedWCFService.PayMentService));
host.Open();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
我尝试在网址http://localhost:8785/service/PayMentService/PayBill的帮助下致电我的服务。但它失败了。我知道服务正在运行,我可以在我的PayMentService构造函数中捕获我的请求,但我无法执行我的PayBill()函数。我尝试了不同的选择,但没有任何作用。任何人都可以给出一些建议......?
提前致谢
答案 0 :(得分:1)
我尝试在网址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/。