WCF REST communication with Windows Phone - wcf

I'm having some trouble regarding a rest request sent from my Windows Phone app via RestSharp to a self hosted WCF service I have made. The request works fine in the emulator and the request returns a result but when I try to perform the request on my app on an actual device it fails and the request does not return anything.
Having done a fair amount of research I have discovered that apparently webHttpBinding is not supported on Windows Phone and therefore I need to add a basicHttpBinding endpoint to my Web.config file. However, when I try to do this I get several errors associated with having more than one endpoint and I can't seem to get any solution to successfully work with both of these. Switching the endpoint to basicHttpBinding and commenting out webHttpBinding also causes an error when I navigate to localhost:81/mywebservice.svc
"System.InvalidOperationException: For request in operation analyseFace to be a stream the operation must have a single parameter whose type is Stream."
asking for the stream that should be attached with a request. It shows a normal help page using the original code and the same address.
My web.config file
<trace>
<listeners>
<add type="Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitorTraceListener, Microsoft.WindowsAzure.Diagnostics, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
name="AzureDiagnostics">
<filter type="" />
</add>
</listeners>
</trace>
</system.diagnostics>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
<behavior name="servicebehavior">
<!--<serviceMetadata httpGetEnabled="true"/>-->
<serviceDebug includeExceptionDetailInFaults="true" httpHelpPageEnabled="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="restbehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="servicebehavior" name="VizageAPIWebRole.vizage">
<endpoint address="" behaviorConfiguration="restbehavior" binding="webHttpBinding" name="RESTEndPoint" contract="VizageAPIWebRole.Ivizage" />
<!--<endpoint address="" binding="basicHttpBinding" contract="VizageAPIWebRole.Ivizage" />-->
</service>
</services>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
<bindings>
<webHttpBinding>
<binding name="RestBinding">
<readerQuotas maxStringContentLength="5242880" maxArrayLength="16384" maxBytesPerRead="4096" />
<security mode="None">
</security>
</binding>
</webHttpBinding>
</bindings>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
My WCF SERVICE
[ServiceContract]
public interface Ivizage
{
[WebInvoke(UriTemplate = "/agerecog/{auth}", Method = "POST")]
VizageResult analyseFace(string auth, Stream dataStream);
}
I'm wondering if someone can help me work out what code I have to edit in my web.config to make this work for both endpoints as I haven't had any luck so far with things I've tried

Your code seems to be a REST service, but basicHttpBinding supports SOAP. So please continue to use webHttpBinding. However, on the Windows Phone side, the WCF cannot be used, it is needed to use WebClient or HttpWebRequest to access a REST service. WCF can just help us to access a SOAP service.
In addition, you mentioned the application works fine in the emulator, so it is unlikely to be a coding issue. It would be better if you can check whether your real phone device is able to access internet. Please also make sure you're using the cloud service's address instead of 127.0.0.1. Local address will not work on a real device.
Best Regards,
Ming Xu.

From the exception and your config - you are using simplified configuration which by default uses basicHttpBinding. You have to explicity define service end point
<services>
<service name="WcfRest.Ivizage">
<endpoint behaviorConfiguration="restbehavior" binding="webHttpBinding"
contract="WcfRest.Ivizage" />
</service>
</services>
Edit: looks like webHttp is not supported

Related

Unable to get MEX to work with my WCF service

I think it may be due to not having/requiring any base address to be specified in the config. Everything else works fine, including publishing WSDL, calling the service, etc. but when I add a MEX endpoint and try to access it, it returns 503 service unavailable.
My web.config looks like this:
<system.serviceModel>
<bindings>
<basicHttpsBinding>
<binding name="ServiceBinding" />
</basicHttpsBinding>
</bindings>
<services>
<service name="MyServices.CatService" behaviorConfiguration="MyBehavior">
<endpoint address="" binding="basicHttpsBinding" bindingConfiguration="ServiceBinding" contract="MyServices.ICatService"></endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MyBehavior">
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="false" httpsGetEnabled="false" />
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
When I browse to my service, I get the message:
You have created a service.
To test this service, you will need to create a client and use it to call the service. You can do this using the svcutil.exe tool from
the command line with the following syntax:
svcutil.exe http://[mypc]/MyServices/CatService.svc/mex
I'm reasonably sure it's to do with the way the endpoints are defined, but I don't know how to fix it. Any ideas?
Solved - the mex example above that I copied and pasted in is http, not https. I changed it mexHttpsBinding and it worked great. I think http didn't work due to some configuration of running http locally but I am not sure.

Unable To Increase Maximum String Content Length Quota (8192)

My colleagues and I have been bashing our heads against a brick wall trying to resolve an issue of sending more than 8192 bytes of data to a web Service. I've tried creating a simple WCF project to test sending some data and yet it still fails at 8192+ bytes. I've read hundreds of web pages and made changes to the web.config file to ensure the elements are sufficiently large enough but it still doesn’t work.
Any suggestions would be appreciated.
We are testing the service using IIS Express on the local host as the web server and using WCFTestClient to send data to the Service. We have also tried using Storm as the client for sending data to the web service.
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<client>
<endpoint binding="basicHttpBinding" bindingConfiguration="NewBinding0"
contract="IService1" name="ClientEndPointName" />
</client>
<services>
<service name="Service1">
<endpoint address="http://localhost:2787" binding="basicHttpBinding"
bindingConfiguration="NewBinding0" name="DROServiceX" bindingName="DROBindingName"
contract="IService1" isSystemEndpoint="false" />
</service>
</services>
<bindings>
<basicHttpBinding>
<binding name="NewBinding0" transferMode="Buffered">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
</binding>
</basicHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="false"
multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
You should configure this on server AND client side. If one of the is not correctly configured, the requests will fail. Your server is configured correctly, so you should have a look at the client configuration. If you realy what to know what is going on, start WCF tracing to see what happens.
WCF Tracing MSDN

wsHttpBinding (with https and aspNetCompatibilityEnabled for routing) metadata error 302

I am working on a WCF service which runs along with ASP.NET MVC 2 application with https port which have been configured using webMatrix-ssl certificate. The service is running perfectly in the browser and could download wsdl as well. But when am trying to load the service into wcftestclient tool and as well as my own client, getting the below error.
Error: Cannot obtain Metadata from 'localhost:44300/MyService' If this is a Windows (R) Communication Foundation service to which you have access, please check that you have enabled metadata publishing at the specified address. For help enabling metadata publishing, please refer to the MSDN documentation at go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata Exchange Error URI: localhost:44300/MyService Metadata contains a reference that cannot be resolved: 'localhost:44300/MyService'. The remote server returned an unexpected response: (302) Found. Too many automatic redirections were attempted.HTTP GET Error URI: localhost:44300/MyService There was an error downloading 'localhost:44300/MyService'. The request failed with the error message:--"
I had added the attribute aspNetCompatibilityEnabled="true" for wcf service routing, if that attribute has been removed all works fine. Initially this service has been configured with basicHttpBinding & had this same issue. After reading this article, I have changed to wshttpBinding as suspected binding could also be the problem, still am getting the same issue, below is my web.config.
Could anyone of you please help me to repair this?
<system.serviceModel>
<extensions>
<behaviorExtensions>
<add name="ExceptionBehavior" type="ServiceExceptionHander.ExceptionHandlerBehaviorExtension, ServiceExceptionHandler" />
</behaviorExtensions>
</extensions>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true" />
<ExceptionBehavior />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="ServiceImplementation.MyService" behaviorConfiguration="ServiceBehavior">
<endpoint binding="wsHttpBinding" bindingConfiguration="securityBinding" contract="ServiceContracts.IMyServiceContracts"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<bindings>
<wsHttpBinding>
<binding name="securityBinding">
<security mode="Transport">
<transport clientCredentialType="None"/>
</security>
</binding>
</wsHttpBinding>
</bindings>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true">
<serviceActivations>
<add factory="System.ServiceModel.Activation.ServiceHostFactory" service="ServiceImplementation.MyService" relativeAddress="MyService.svc"/>
</serviceActivations>
</serviceHostingEnvironment>
</system.serviceModel>

How do I troubleshoot a 404 when calling my REST service?

I created a WCF service and exposed two endpoints; one for SOAP and one for REST. The SOAP endpoint works and is reachable from web clients, console apps, etc. The REST endpoint was created this morning and doesn't work. I confirmed the SOAP service is still reachable so this problem is specific to the REST portion or configuration changes made this morning.
In the browser, I try to navigate to http://ABC:99/json/categories. I get a 404 - File or directory not found.
I added the following attribute to a method in the class which implements the service interface:
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "categories")]
and this to another method:
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "reports/{categoryId}")]
The relevant portions of the config file are here:
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="NewBinding0" maxBufferSize="524288" maxReceivedMessageSize="524288" transferMode="StreamedResponse"
closeTimeout="00:10:00" openTimeout="00:10:00" sendTimeout="00:10:00" receiveTimeout="00:10:00" />
</basicHttpBinding>
</bindings>
<services>
<service name="XXX.ReportGenerator">
<endpoint address="" binding="basicHttpBinding" bindingConfiguration="NewBinding0"
contract="XXX.YYY.IReportGenerator">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
<service name="ReportingSvc.JSON">
<endpoint address="json" binding="webHttpBinding"
contract="XXX.YYY.ReportingSvc.IReportGenerator" behaviorConfiguration="jsonEndpoint" />
<host>
<baseAddresses>
<add baseAddress="http://ABC:99/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="jsonEndpoint">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
Any suggestions are greatly appreciated! Thanks!
I can't give you specific help with REST services in WCF as I've no experience there, but WCF Tracing has been a lifesaver for me in multiple instances in the past. Simply follow instructions to add the following to your WCF service's web.config:
<configuration>
<system.diagnostics>
<sources>
<source name="System.ServiceModel"
switchValue="Information, ActivityTracing"
propagateActivity="true">
<listeners>
<add name="traceListener"
type="System.Diagnostics.XmlWriterTraceListener"
initializeData= "c:\log\Traces.svclog" />
</listeners>
</source>
</sources>
</system.diagnostics>
</configuration>
Obviously you'd change the path for your log as necessary and make sure the correct permissions were set on the folder you set, but that's essentially it. Once the trace starts being logged, you can use the Service Trace Viewer, which I believe is installed by default, to view the trace and see if you can't pinpoint the source of your problem.
EDIT
Please note though, that service tracing is super resource-intensive, so only turn it on when you need it to debug problems and then turn it off when you don't need it anymore.
If your method is intended to just perform a GET operation its better to use the WebGet attribute rather than using WebInvoke and specifying your method to be GET.
Also the URL for your service for the first method would be as below:
http://ABC:99/categories/json
URL for 2nd method would be
http://ABC:99/reports/5/json
This is because you are defining the address as "json" as your endpoint address which gets appended to the base URL + Resource URL + endpoint address.

WCF over IIS through load balancer reports wrong base address

I'm trying to launch a WCF service over SSL on IIS 6 through a load balancer. My initial problem was an obvious and pretty well discussed one - the address shown on the WSDL page pointed to https://SERVERNAME/WebServices/mydomainws.svc instead of www.mydomain.com. The answer to this problem is to add a host header value in IIS. I did that and it worked... sort of. I now get http://www.mydomain.com/WebServices/mydomainws.svc when viewing the wsdl in a browser. If I click on that link (the non-ssl link) I get a service definition that again references the server name.
The next oft recommended remedy is to use WCF Extras which provides an extension that allows you to specify a base address. But setting that config entry only updated the soap12:address. The EndPointReference address is still using the machine name.
To summarize: WSDL as viewed in web browser at https://www.mydomain.com/WebServices/mydomainws.svc: http://www.mydomain.com/WebServices/mydomainws.scv
Clicking the above link brings me to an actual wsdl file with the following service entry:
https://ServerName/WebServices/mydomainws.svc
My server config file has the following serviceModel entries:
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="TransportSecurity">
<security mode="Transport">
<message clientCredentialType="None"/>
<transport clientCredentialType="None"/>
</security>
</binding>
</wsHttpBinding>
</bindings>
<services>
<service name="mydomain.ws.mydomainws" behaviorConfiguration="mydomainwsBehavior">
<!-- Service Endpoints -->
<endpoint address="" **behaviorConfiguration="CorrectEndPoint"** binding="wsHttpBinding" bindingConfiguration="TransportSecurity" contract="mydomain.ws.Imydomainws"/>
<endpoint address="mex" **behaviorConfiguration="CorrectEndPoint"** binding="mexHttpsBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="mydomainwsBehavior">
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true" />
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
**<behavior name="CorrectEndPoint">
<wsdlExtensions location="https://www.mydomain.com/WebServices/mydomainws.svc" singleFile="true"/>
</behavior>**
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
<extensions>
<behaviorExtensions>
<add name="wsdlExtensions" type="WCFExtras.Wsdl.WsdlExtensionsConfig, WCFExtras, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
</behaviorExtensions>
</extensions>
</system.serviceModel>
Can anyone point me in the right direction?
Thanks,
George
This should be handeled by new behavior useRequestHeadersForMetadataAddress. Try to add this to your service behavior:
<serviceBehaviors>
<behavior name="LoadBalancedBehavior">
<serviceMetadata httpGetEnabled="true" />
<useRequestHeadersForMetadataAddress>
<defaultPorts>
<add scheme="http" port="80" />
<add scheme="https" port="443" />
</defaultPorts>
</useRequestHeadersForMetadataAddress>
<!-- Other service behaviors as necesary -->
</behavior>
</serviceBehaviors>
This behavior is availabel in WCF 4.0 and should be available as KB for WCF 3.x.