Help Please. I was trying to access WCF service exposed to a service bus relay endpoint using HttpWebRequest (REST).
I successfully got a token from ACS via OAuth WRAP Protocol. Using that token as a authorization in a request header, I created a WebRequest to communicate with the WCF service with an endpoint configured as WebHttpRelayBinding and a WCF service method applied
with OperationContractAttribute and WebGetAttribute.
When I run the client application I got following error:
The message with To 'https://namespace.servicebus.windows.net/Student/GetInfo/GetStudentInfo/1' cannot be processed at the receiver, due to an AddressFilter
mismatch at the EndpointDispatcher. Check that the sender and receiver's EndpointAddresses agree.
I googled and found a suggestion to apply following attribute to the service class:
[ServiceBehavior(AddressFilterMode=AddressFilterMode.Any)]
Although this resolved the previous error, but now the client application is ending up with following error:
The message with Action 'GET' cannot be processed at the receiver, due to a ContractFilter mismatch at the EndpointDispatcher. This may be because of either a contract mismatch (mismatched Actions between sender and receiver) or a binding/security mismatch
between the sender and the receiver. Check that sender and receiver have the same contract and the same binding (including security requirements, e.g. Message, Transport, None).
I found that this could be due to following reasons:
- You have different contracts between client and sender.
- You're using a different binding between client and sender.
- The message security settings are not consistent between client and sender.
As far as contract is concerned, I have exposed an operation contract via a WebGetAttribute defining a REST uri to access the method.
I don't know if it is necessary to provide binding information while accessing a WCF service through HttpWebRequest (REST). If so, How?
I haven't applied any security settings in the service except Azure Service Bus shared secret that I have provided in the header of an HttpWebRequest.
Below is my code for both Service and a client consuming the service.
WCF Service Code:
[ServiceContract]interfaceIStudentInfo{[OperationContract][WebGet(ResponseFormat=WebMessageFormat.Xml,UriTemplate="/GetStudentInfo/{studentId}")]stringGetStudentInfo(string studentId);}[ServiceBehavior(AddressFilterMode=AddressFilterMode.Any)]privateclassStudentInfo:IStudentInfo{stringIStudentInfo.GetStudentInfo(string studentId){string returnString =null;// .....return returnString;}}publicvoidRun(){Console.WriteLine("LISTENER");Console.WriteLine("========");string serviceNamespace ="namespace";string issuerName ="owner";string issuerKey ="key";string servicePath ="Student/GetInfo";ServiceHost sh =newServiceHost(typeof(StudentInfo));// BindingWebHttpRelayBinding binding2 =newWebHttpRelayBinding();Uri uri =ServiceBusEnvironment.CreateServiceUri(Uri.UriSchemeHttps, serviceNamespace, servicePath);Console.WriteLine("Service Uri: "+ uri);Console.WriteLine();
sh.AddServiceEndpoint(typeof(IStudentInfo), binding2, uri);// Create the ServiceRegistrySettings behavior for the endpoint.var serviceRegistrySettings =newServiceRegistrySettings(DiscoveryType.Public);// Create the shared secret credentials object for the endpoint matching the // Azure access control services issuer var sharedSecretServiceBusCredential =newTransportClientEndpointBehavior(){TokenProvider=TokenProvider.CreateSharedSecretTokenProvider(issuerName, issuerKey)};// Add the service bus credentials to all endpoints specified in configuration.foreach(var endpoint in sh.Description.Endpoints){
endpoint.Behaviors.Add(serviceRegistrySettings);
endpoint.Behaviors.Add(sharedSecretServiceBusCredential);}
sh.Open();Console.WriteLine("Press ENTER to close");Console.ReadLine();
sh.Close();}
Service Consuming Code:
staticvoidMain(string[] args){var studentId ="1";string _token =GetToken();Console.WriteLine(_token);// Create and configure the Requestvar httpWebRequest =(HttpWebRequest)WebRequest.Create("https://namespace.servicebus.windows.net/Student/GetInfo/GetStudentInfo/"+ studentId);
httpWebRequest.ContentType="text/json";
httpWebRequest.Method="GET";
httpWebRequest.Headers.Add(HttpRequestHeader.Authorization,string.Format("WRAP access_token=\"{0}\"", _token));// Get the response using the RequestHttpWebResponse response = httpWebRequest.GetResponse()asHttpWebResponse;// Read the stream from the response objectStream stream = response.GetResponseStream();StreamReader reader =newStreamReader(stream);// Read the result from the stream readerstring result = reader.ReadToEnd();Console.WriteLine("Result: "+ result);Console.ReadLine();}staticstringGetToken(){string base_address =string.Format("https://namespace-sb.accesscontrol.windows.net");string wrap_name ="owner";string wrap_password ="key";string wrap_scope ="http://namespace.servicebus.windows.net/";
blog: <a href="http://technologynotesforyou.wordpress.com">http://technologynotesforyou.wordpress.com</a> | skype: ali.net.pk