【发布时间】: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