具有WebHttpBinding的ChannelFactory在执行ServiceContractOperationCo



我有一个通过WebServiceHost托管的服务,我需要将一些调用委托给web上的其他REST服务。

我构建了一个ClientBase具体类来处理这个问题。流程如下:

http://localhost:8000/users/my@email.com->我的WebServiceHost实例->ClientBase->REST服务

一切都很好,直到我意识到所有来自ClientBase的调用都使用POST作为动词。为了确保我没有对ClientBase做任何愚蠢的事情,我手动构建了一个ChannelFactory并使用了它。运气不好,无论ClientBase、ChannelFactory,甚至ServiceContract装饰如何,每个调用都仍然使用POST。

然后我开始隔离代码,并意识到当最初的调用不是来自我的WebServiceHost正在处理的请求时,我的简单ChannelFactory就可以工作了。

这是一个经过提炼的Program.cs,它展示了确切的问题,Program.Main的MakeGetCall()按预期工作,但MyService.GetUser的调用将始终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";
    }
}

在这里找到了一个工作:

http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/03a2b109-c400-49d4-891e-03871ae0d083/

最新更新