문제

I have a service that is hosted via WebServiceHost and I need to delegate some of the calls to other REST services on the web.

I built a ClientBase concrete class to handle this. The flow looks like this:

http://localhost:8000/users/my@email.com -> My WebServiceHost instance -> ClientBase -> REST service

Everything was working well, until I realized that ALL calls coming from ClientBase were using POST as the verb. In order to make sure I wasn't doing anything silly with ClientBase I built a ChannelFactory manually and used that. No luck, every call still used POST regardless of ClientBase, ChannelFactory, and even ServiceContract decorations.

I then started isolating code and realized my simple ChannelFactory worked when the original call wasn't coming from within a request my WebServiceHost was handling.

Here's a distilled Program.cs that exhibits the exact problem, the MakeGetCall() from Program.Main works as intended, but the call from MyService.GetUser will always POST:

class Program
{
    static void Main(string[] args)
    {
        //Program.MakeGetCall(); //This works as intended even when changing the WebInvoke attribute parameters

        WebServiceHost webServiceHost = new WebServiceHost(typeof(MyService), new Uri("http://localhost:8000/"));

        ServiceEndpoint serviceEndpoint = webServiceHost.AddServiceEndpoint(typeof(IMyServiceContract), new WebHttpBinding(), "");

        webServiceHost.Open();

        Console.ReadLine();
    }

    public static void MakeGetCall()
    {
        ServiceEndpoint endpoint = new ServiceEndpoint(
            ContractDescription.GetContract(typeof(IMyServiceContract)),
            new WebHttpBinding(),
            new EndpointAddress("http://posttestserver.com/post.php"));

        endpoint.Behaviors.Add(new WebHttpBehavior());

        ChannelFactory<IMyServiceContract> cf = new ChannelFactory<IMyServiceContract>(endpoint);

        IMyServiceContract test = cf.CreateChannel();

        test.GetUser("test");
    }

}

[ServiceContract]
public interface IMyServiceContract
{
    [OperationContract]
    [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json,
        UriTemplate = "/users/{emailAddress}")]
    string GetUser(string emailAddress);
}

public class MyService : IMyServiceContract
{
    public string GetUser(string emailAddress)
    {
        Program.MakeGetCall(); //This will ALWAYS POST no matter if you are using [WebInvoke(Method="GET")] or even [WebGet]

        return "foo";
    }
}
도움이 되었습니까?
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top