thecaptain0220
thecaptain0220

Reputation: 2168

Restful service consuming WCF self hosted service

I am currently working with an application that is using a restful service. There is another application that has a self hosted WCF service running. I would like to consume the self hosted service from the restful service but I'm running into an issue. I am getting a (405) Method Not Allowed.

Here is how the self hosted service is created and hosted

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

Here is how I am trying to consume the function in the 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);

Here is an example of the service

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

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

I did some research and I'm guessing its because I'm calling from a restful service. I have tried using [WebGet()] and [WebInvoke(Method="GET")] but it didnt seem to make a difference. Not sure what I am missing.

Upvotes: 0

Views: 471

Answers (1)

vibhu
vibhu

Reputation: 1695

I have tried to simulate your scenario (from whatever I could comprehend from the description) and it worked fine -

Self hosted service code

 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 call to the self hosted service after adding the reference to the self hosted 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();

}

It returns me

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

Upvotes: 1

Related Questions