MonoTouch WCF REST Error Creating Channel
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<IAuthenticationServic开发者_StackOverflow社区e> 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.
精彩评论