使用WCF自托管服务的休息服务

时间:2013-07-11 18:55:11

标签: wcf web-services rest

我目前正在使用正在使用restful服务的应用程序。还有另一个应用程序正在运行自托管WCF服务。我想从宁静的服务中使用自托管服务,但我遇到了一个问题。我得到了一个(405)方法不允许。

以下是如何创建和托管自托管服务

ServiceHost host = new ServiceHost(typeof(LiveService));
host.Open();

以下是我尝试在restful service中使用该函数的方法

BinaryMessageEncodingBindingElement binaryMessageEncoding = new BinaryMessageEncodingBindingElement();
HttpTransportBindingElement httpTransport = new HttpTransportBindingElement() { MaxBufferSize = int.MaxValue, MaxReceivedMessageSize = int.MaxValue };
CustomBinding ServiceCustomBinding = new CustomBinding(binaryMessageEncoding, httpTransport);

EndpointAddress ServiceEndpointAddress = new EndpointAddress(string.Format("http://{0}/LiveService", host));

LiveWebServiceClient client = new LiveWebServiceClient(ServiceCustomBinding, ServiceEndpointAddress);

以下是服务的示例

[ServiceContract]
public interface ILiveService
{
    [OperationContract]
    string Hello();
}

public string Hello()
{
    return "Hello";
}

我做了一些研究,我猜它是因为我正在通过一项宁静的服务来打电话。我曾尝试使用[WebGet()]和[WebInvoke(Method =“GET”)]但它似乎没有什么区别。不确定我错过了什么。

1 个答案:

答案 0 :(得分:1)

我试图模拟你的场景(从我能从描述中理解的任何东西)并且它运行良好 -

自托管服务代码

 namespace SelfHostedService
  {
   [ServiceContract]
   internal interface ILiveService
    {
      [OperationContract]
      string Hello();
    }

public class LiveService:ILiveService
{
    public string Hello()
    {
        return "Hello";
    }
}
}
        static void Main(string[] args)
    {
        var binaryMessageEncoding = new TextMessageEncodingBindingElement();
        var httpTransport = new HttpTransportBindingElement() { MaxBufferSize = int.MaxValue, MaxReceivedMessageSize = int.MaxValue };
        var ServiceCustomBinding = new CustomBinding(binaryMessageEncoding, httpTransport);

        ServiceHost host = new ServiceHost(typeof(LiveService), new Uri("http://localhost:3239/LiveService"));
        host.AddServiceEndpoint(typeof (ILiveService), ServiceCustomBinding, "");
        var smb = new ServiceMetadataBehavior();
        smb.HttpGetEnabled = true;
        host.Description.Behaviors.Add(smb);
        host.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName, MetadataExchangeBindings.CreateMexHttpBinding(), "mex");

        host.Open();
        Console.ReadLine();
    }

在添加对自托管服务的引用后,Restful Service调用自托管服务 -

[ServiceContract]
public interface IService1
{

    [OperationContract]
    [WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
  }

    public string ReturnFromSelfHostService()
    {
        var binaryMessageEncoding = new TextMessageEncodingBindingElement();
        var httpTransport = new HttpTransportBindingElement() { MaxBufferSize = int.MaxValue, MaxReceivedMessageSize = int.MaxValue };
        var ServiceCustomBinding = new CustomBinding(binaryMessageEncoding, httpTransport);
        var ServiceEndpointAddress = new EndpointAddress(string.Format("http://{0}/LiveService", "localhost:3239"));

        var client = new LiveServiceClient(ServiceCustomBinding, ServiceEndpointAddress);
        return client.Hello();

    }
    string ReturnFromSelfHostService();

}

它让我回头

 <ReturnFromSelfHostServiceResponse xmlns="http://tempuri.org/">
  <ReturnFromSelfHostServiceResult>Hello</ReturnFromSelfHostServiceResult> 
  </ReturnFromSelfHostServiceResponse>