【问题标题】:Restful service consuming WCF self hosted service使用 WCF 自托管服务的 Restful 服务
【发布时间】:2013-07-10 04:29:34
【问题描述】:

我目前正在使用一个使用 RESTful 服务的应用程序。还有另一个运行自托管 WCF 服务的应用程序。我想从 restful 服务中使用自托管服务,但我遇到了问题。我得到一个 (405) Method Not Allowed。

这是自托管服务的创建和托管方式

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

这是我尝试在 restful 服务中使用该功能的方式

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")] 但似乎没有什么不同。不知道我错过了什么。

【问题讨论】:

  • 您在 string.Format("http://{0}/LiveService", host) 中设置的主机值是多少?
  • 我传入了值。这最终成为问题,我没有包括自托管服务的端口。

标签: wcf web-services rest


【解决方案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>

【讨论】:

  • 当然,您可以在自定义绑定中使用 BinaryEncoding。
  • 感谢您抽出宝贵时间试用。看看你的代码,它让我面对我做错了什么。我忘记将端口添加到地址中。我使用相同的代码连接到不需要端口的托管服务,却忘记为自托管服务添加它。我被这个错误分心了。这似乎是一个奇怪的错误。似乎它根本找不到服务。再次感谢您的时间和帮助。
猜你喜欢
  • 2020-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-10
  • 2011-11-07
  • 1970-01-01
相关资源
最近更新 更多