MonoTouch WCF REST 创建通道时出错

发布于 2024-10-21 02:06:44 字数 2502 浏览 7 评论 0原文

我正在尝试通过 MonoTouch 访问 WCF REST 服务。我无法使用 ChannelFactory,因为在 MonoTouch 中无法生成动态代码,并且因为我正在访问 RESTful 服务,所以我也无法使用 svcutil 来构建客户端类。

这让我不得不手动构建客户端类。我已经走了很远,但遇到了问题,这是我的代理类代码:

public class AuthenticationClient : System.ServiceModel.ClientBase<IAuthenticationService>, IAuthenticationService
{
    public AuthenticationClient(Binding binding, EndpointAddress baseAddress) : base(binding, baseAddress)
    { } 

    public AuthenticationResult AuthenticateUser (AccountCredentials credentials)
    {
        return base.Channel.AuthenticateUser(credentials);
    }

    protected override IAuthenticationService CreateChannel ()
    {
        // This method must be overriden in MonoTouch to prevent using a ChannelFactory
        return new AuthenticationChannel(this);
    }

    private class AuthenticationChannel : ChannelBase<IAuthenticationService>, IAuthenticationService
    {
         public AuthenticationChannel(System.ServiceModel.ClientBase<IAuthenticationService> client) : 
            base(client)
        { }

        public AuthenticationResult AuthenticateUser (AccountCredentials credentials)
        {
            object[] _args = new object[1];
            _args[0] = credentials;

            return (AuthenticationResult)base.Invoke("AuthenticateUser", _args); // Exception thrown here       
        }
    }   
}

接口 IAuthenticationService 使用属性来指定端点 Uri:

[ServiceContract]
public interface IAuthenticationService
{
    [OperationContract]
    [WebInvoke(Method = "POST", UriTemplate = "/Authenticate/User")]
    AuthenticationResult AuthenticateUser(AccountCredentials credentials);
}

这是我使用代理类的方式:(

var serviceHost = "http://mydomain.com/";
var servicePath = "Services/Authentication";

Uri hostUri = new Uri(serviceHost);
Uri serviceUri = new Uri(hostUri, servicePath);

var binding = new WebHttpBinding();

var client = new AuthenticationClient(binding, new EndpointAddress(serviceUri));

client.Endpoint.Behaviors.Add(new MyWebHttpBehaviour());

var credentials = new AccountCredentials
{
    Username = "myusername",
    Password = "mypassword"
};

var result = client.AuthenticateUser(credentials);      

出于某种原因,WebHttpBehavior 不实现 IEndpointBehavior,因此我创建了自己的类,该类继承自 WebHttpBehavior 并且也实现了 IEndpointBehavior)。

我收到的异常是:

System.InvalidOperationException:在传输上启用手动寻址时,每个请求消息都必须设置其目标地址。

有人可以帮忙吗?

干杯, 安东尼.

I'm trying to access WCF REST services via MonoTouch. I am unable to use a ChannelFactory, as dynamic code generation is not possible in MonoTouch, and because I'm accessing RESTful services, I'm also unable to use svcutil to build the client classes.

This leaves me with building the client classes manually. I've gotten quite far but have hit a problem, here is my code for the proxy class:

public class AuthenticationClient : System.ServiceModel.ClientBase<IAuthenticationService>, IAuthenticationService
{
    public AuthenticationClient(Binding binding, EndpointAddress baseAddress) : base(binding, baseAddress)
    { } 

    public AuthenticationResult AuthenticateUser (AccountCredentials credentials)
    {
        return base.Channel.AuthenticateUser(credentials);
    }

    protected override IAuthenticationService CreateChannel ()
    {
        // This method must be overriden in MonoTouch to prevent using a ChannelFactory
        return new AuthenticationChannel(this);
    }

    private class AuthenticationChannel : ChannelBase<IAuthenticationService>, IAuthenticationService
    {
         public AuthenticationChannel(System.ServiceModel.ClientBase<IAuthenticationService> client) : 
            base(client)
        { }

        public AuthenticationResult AuthenticateUser (AccountCredentials credentials)
        {
            object[] _args = new object[1];
            _args[0] = credentials;

            return (AuthenticationResult)base.Invoke("AuthenticateUser", _args); // Exception thrown here       
        }
    }   
}

The interface IAuthenticationService uses attributes to specify the endpoint Uri's:

[ServiceContract]
public interface IAuthenticationService
{
    [OperationContract]
    [WebInvoke(Method = "POST", UriTemplate = "/Authenticate/User")]
    AuthenticationResult AuthenticateUser(AccountCredentials credentials);
}

Here is how I'm using the proxy class:

var serviceHost = "http://mydomain.com/";
var servicePath = "Services/Authentication";

Uri hostUri = new Uri(serviceHost);
Uri serviceUri = new Uri(hostUri, servicePath);

var binding = new WebHttpBinding();

var client = new AuthenticationClient(binding, new EndpointAddress(serviceUri));

client.Endpoint.Behaviors.Add(new MyWebHttpBehaviour());

var credentials = new AccountCredentials
{
    Username = "myusername",
    Password = "mypassword"
};

var result = client.AuthenticateUser(credentials);      

(For some reason WebHttpBehavior does not implement IEndpointBehavior, so I created my own class that inherits from WebHttpBehavior and also implements IEndpointBehavior).

The exception I receive is:

System.InvalidOperationException: When manual addressing is enabled on the transport, every request messages must be set its destination address.

Can anyone help?

Cheers,
Anthony.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文