0

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

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

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

4

1 回答 1

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>
于 2013-07-11T21:11:54.537 回答