none
Routing Json using System.ServiceModel.Routing.RoutingService RRS feed

  • Question

  • Good Day

    I am new to using the "System.ServiceModel.Routing.RoutingService".

    I have created a test application using WCF and ASP.NET. I this application, I have 2 buttons. the first is a normal echo service, the second is based on json. When the ASP.NET app comunicates with the WCF service directly, it works correctly. 

    I then created a proxy service using System.ServiceModel.Routing.RoutingService, the echo service is working correctly when routed through the proxy, but when I try to route the json through the proxy, I get the following error "Message=The remote server returned an error: (415) Cannot process the message because the content type 'application/json' was not the expected type 'text/xml; charset=utf-8'.."

    here is the proxy server's configuration

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <system.serviceModel>
        <services>
          <service name="System.ServiceModel.Routing.RoutingService" behaviorConfiguration="extBehavior">
            <endpoint name="endpointRR" address="" binding="basicHttpBinding" contract="System.ServiceModel.Routing.IRequestReplyRouter" />
            <endpoint name="endpointWebRR" address="json" binding="webHttpBinding" contract="System.ServiceModel.Routing.IRequestReplyRouter" bindingConfiguration="webHttpBindingWithJsonP" behaviorConfiguration="webHttpBehavior" />
            <endpoint name="entpointDSR" address="" binding="basicHttpBinding" contract="System.ServiceModel.Routing.IDuplexSessionRouter" />
            <endpoint name="entpointNotify" address="Notify" binding="basicHttpBinding" contract="System.ServiceModel.Routing.ISimplexDatagramRouter" />
          </service>
        </services>
        <client>
          <endpoint name="CustomersService" address="http://localhost:46232/CustomersService.svc" binding="webHttpBinding" contract="*" />
          <endpoint name="EchoService" address="http://localhost:46232/EchoService.svc" binding="basicHttpBinding" contract="*" />
        </client>
        <behaviors>
          <serviceBehaviors>
            <behavior name="webBehavior">
              <serviceDebug includeExceptionDetailInFaults="True" />
              <serviceMetadata httpGetEnabled="True" />
              <routing filterTableName="routingTable1" routeOnHeadersOnly="False" />
            </behavior>
            <behavior name="extBehavior">
              <serviceDebug includeExceptionDetailInFaults="True" />
              <serviceMetadata httpGetEnabled="True" />
              <routing filterTableName="routingTable1" routeOnHeadersOnly="False" />
            </behavior>
            <behavior name="mex">
              <serviceDebug includeExceptionDetailInFaults="True" />
              <serviceMetadata httpGetEnabled="True" />
            </behavior>
            <behavior name="">
              <serviceDebug includeExceptionDetailInFaults="True" />
              <serviceMetadata httpGetEnabled="True" />
            </behavior>
          </serviceBehaviors>
          <endpointBehaviors>
            <behavior name="webHttpBehavior">
              <webHttp automaticFormatSelectionEnabled="true" />
            </behavior>
          </endpointBehaviors>
        </behaviors>
        <bindings>
          <webHttpBinding>
            <binding name="webHttpBindingWithJsonP" crossDomainScriptAccessEnabled="True" />
          </webHttpBinding>
          <basicHttpBinding>
            <binding name="basicHttpCustom" maxReceivedMessageSize="2000000" />
          </basicHttpBinding>
          <netMsmqBinding>
            <binding name="netMsmqBinding" exactlyOnce="True" deadLetterQueue="System">
              <security mode="None" />
            </binding>
          </netMsmqBinding>
        </bindings>
        <routing>
          <filters>
            <clear />
            <filter name="aa" filterType="EndpointName" filterData="endpointRR" filter1="" filter2="" customType="" />
            <filter name="bb" filterType="EndpointName" filterData="endpointWebRR" filter1="" filter2="" customType="" />
          </filters>
          <filterTables>
            <clear />
            <filterTable name="routingTable1">
              <clear />
              <add filterName="bb" endpointName="CustomersService"/>
              <add filterName="aa" endpointName="EchoService"/>
            </filterTable>
          </filterTables>
          <backupLists>
            <clear />
            <backupList name="Test1">
              <clear />
              <add endpointName="Test1" />
            </backupList>
          </backupLists>
          <namespaceTable>
            <clear />
            <add prefix="rk" namespace="urn:tempuri.org" />
          </namespaceTable>
        </routing>
      </system.serviceModel>
    </configuration>

    here is the wcf server's configuration


    <configuration>
      <system.web>
        <compilation debug="true" targetFramework="4.0" />
      </system.web>

      <system.serviceModel>
        <bindings>
          <webHttpBinding>
            <binding name="webHttpBindingWithJsonP" crossDomainScriptAccessEnabled="true" />
          </webHttpBinding>
          <basicHttpBinding>
            <binding name="basicHttpCustom"
                     maxReceivedMessageSize="2000000" />
          </basicHttpBinding>
        </bindings>
        <services>
          <service name="WCF4Configuration.EchoService">
            <endpoint address=""
                      binding="basicHttpBinding"
                      contract="WCF4Configuration.IEchoService" />
          </service>
          <service name="WCF4Configuration.CustomersService">
            <endpoint address="" 
                      behaviorConfiguration="webHttpBehavior"
                      binding="webHttpBinding" 
                      bindingConfiguration="webHttpBindingWithJsonP"
                      contract="WCF4Configuration.ICustomersService" />
          </service>
        </services>
        <client>
          <endpoint address="http://localhost:46232/EchoService.svc"
                    binding="basicHttpContextBinding"
                    contract="WCF4Configuration.IEchoService"
                    name="EchoService" />
          <endpoint address="mex"
                    binding="mexHttpBinding"
                    contract="WCF4Configuration.EchoService"
                    name="mexEchoService" />
          <endpoint address="http://localhost:46232/CustomersService.svc"
                    binding="webHttpBinding"
                    contract="WCF4Configuration.ICustomersService"
                    name="CustomersService" />
          <endpoint address="mex"
                    binding="mexHttpBinding"
                    contract="IMetadataExchange"
                    name="mexCustomersService" />
        </client>
        <behaviors>
          <serviceBehaviors>
            <behavior name="">
              <serviceMetadata httpGetEnabled="true" />
              <serviceDebug includeExceptionDetailInFaults="true" />
            </behavior>
            <behavior name="MyServiceTypeBehaviors">
              <serviceMetadata httpGetEnabled="true" />
              <serviceDebug includeExceptionDetailInFaults="true" />
            </behavior>
            <behavior name="mex">
              <serviceDebug includeExceptionDetailInFaults="false"/>
              <serviceMetadata />
            </behavior>
          </serviceBehaviors>
          <endpointBehaviors>
            <behavior name="webHttpBehavior">
              <webHttp />
            </behavior>
          </endpointBehaviors>
        </behaviors>
        <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
      </system.serviceModel>
    </configuration>

    here is the client's configuration

    <system.serviceModel>
        <standardEndpoints>
          <webHttpEndpoint>
            <!-- 
          Configure the WCF REST service base address via the global.asax.cs file and the default endpoint 
          via the attributes on the <standardEndpoint> element below
        -->
            <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true" />
          </webHttpEndpoint>
        </standardEndpoints>
        <bindings>
          <basicHttpBinding>
            <binding name="BasicHttpBinding_IEchoService" />
          </basicHttpBinding>
          <webHttpBinding>
            <binding name="webHttpBinding" crossDomainScriptAccessEnabled="true" />
          </webHttpBinding>
        </bindings>
        <behaviors>
          <endpointBehaviors>
            <behavior name="webHttpBehavior">
              <webHttp />
            </behavior>
          </endpointBehaviors>
        </behaviors>
        <client>
          <!--<endpoint address="http://localhost:46232/CustomersService.svc" behaviorConfiguration="webHttpBehavior"
            binding="webHttpBinding" contract="CustomerServiceReference.ICustomersService"
            name="web;HttpBinding_ICustomersService" />
          <endpoint address="http://localhost:46232/EchoService.svc" binding="basicHttpBinding"
            contract="EchoServiceReference.IEchoService" name="BasicHttpBinding_IEchoService" />-->      
          <!--<endpoint address="http://localhost.fiddler:46232/CustomersService.svc" behaviorConfiguration="webHttpBehavior"
            binding="webHttpBinding" contract="CustomerServiceReference.ICustomersService"
            name="webHttpBinding_ICustomersService" />
          <endpoint address="http://localhost.fiddler:46232/EchoService.svc" binding="basicHttpBinding"
            contract="EchoServiceReference.IEchoService" name="BasicHttpBinding_IEchoService" />-->
          <endpoint address="http://dev.nzpost.co.nz/proxy/proxy.svc/json" behaviorConfiguration="webHttpBehavior"
            binding="webHttpBinding" contract="CustomerServiceReference.ICustomersService"
            name="webHttpBinding_ICustomersService" />
          <endpoint address="http://dev.nzpost.co.nz/proxy/proxy.svc" binding="basicHttpBinding"
            contract="EchoServiceReference.IEchoService" name="BasicHttpBinding_IEchoService" />
        </client>
      </system.serviceModel>

    the code used to query the json 

                using (CustomersServiceClient client = new CustomersServiceClient())
                {
                    //var data1 = MakeRequest(client.Endpoint.Address.ToString() + "/GetCustomer?id=" + Message.Text.Trim());
                    //var data2 = MakeRequestAsList(client.Endpoint.Address.ToString() + "/GetCustomers");
                    //var data2 = MakeRequestAsList(client.Endpoint.Address.ToString() + "?op=GetCustomers");
                    string serviceBaseUrl = client.Endpoint.Address.ToString();
                    string resourceUrl = "/GetCustomers";
                    string method = "POST";
                    string jsonText = "{\"value\":{}}";
                    lblCustomerInfoReceived.Text = UseHttpWebApproach(serviceBaseUrl, resourceUrl, method, jsonText);
                }

    private string UseHttpWebApproach(string serviceUrl, string resourceUrl, string method, string requestBody)
            {
                string responseMessage = null;
                var request = WebRequest.Create(string.Concat(serviceUrl, resourceUrl)) as HttpWebRequest;
                if (request != null)
                {
                    request.ContentType = "application/json";
                    request.Method = method;
                }

                //var objContent = HttpContentExtensions.CreateDataContract(requestBody);
                if (method == "POST" && requestBody != null)
                {
                    byte[] requestBodyBytes = ToByteArrayUsingJsonContractSer(requestBody);
                    request.ContentLength = requestBodyBytes.Length;
                    using (Stream postStream = request.GetRequestStream())
                        postStream.Write(requestBodyBytes, 0, requestBodyBytes.Length);

                }

                if (request != null)
                {
                    var response = request.GetResponse() as HttpWebResponse;
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        Stream responseStream = response.GetResponseStream();
                        if (responseStream != null)
                        {
                            var reader = new StreamReader(responseStream);

                            responseMessage = reader.ReadToEnd();
                        }
                    }
                    else
                    {
                        responseMessage = response.StatusDescription;
                    }
                }
                return responseMessage;
            }

            private static byte[] ToByteArrayUsingJsonContractSer(string requestBody)
            {
                byte[] bytes = null;
                var serializer1 = new DataContractJsonSerializer(typeof(string));
                var ms1 = new MemoryStream();
                serializer1.WriteObject(ms1, requestBody);
                ms1.Position = 0;
                var reader = new StreamReader(ms1);
                bytes = ms1.ToArray();
                return bytes;
            }

    I hope that someone can assist me

    Ronald

    Monday, May 13, 2013 2:56 AM

Answers