locked
WCF service with OperationContract returning complex object RRS feed

  • Question

  • User1663359597 posted

    Hello

    I am working on WCF service that will have a methot which result is complex object. Problem exists when I am trying to set ResponseFormat to Json. In this case, when I am invoking HTTP GET on localhost, f.e. localhost:4444/Service.svc/item/14  my url after processing is changed to www.localhost.com:4444/..., which is of course, wrong.

    Note, that this happens only in case of Json.

    Here are my sources:

    Web.config

    <?xml version="1.0"?>
    <configuration>
      <appSettings/>
      <system.web>
        <compilation debug="true" targetFramework="4.0"/>
        <httpRuntime/>
      </system.web>
      <system.serviceModel>
        <behaviors>
          <serviceBehaviors>
            <behavior>
              <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
              <serviceMetadata httpGetEnabled="true" 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="true"/>
              <useRequestHeadersForMetadataAddress />
            </behavior>
          </serviceBehaviors>
          <endpointBehaviors>
            <behavior name="RestBehaviour">
              <webHttp />
            </behavior>
          </endpointBehaviors>
        </behaviors>
        <protocolMapping>
          <add binding="basicHttpsBinding" scheme="https"/>
        </protocolMapping>
        <services>
          <service name="PromoCenyParseService.PromoCenyParseService">
            <endpoint address="" behaviorConfiguration="RestBehaviour" binding="webHttpBinding" contract="PromoCenyParseService.IPromoCenyParseService"/>
            <host>
              <baseAddresses>
                <add baseAddress="localhost:6833/PromoCenyParseService.svc" />
    
              </baseAddresses>
    
            </host>
          </service>
        </services>
      </system.serviceModel>
      <system.webServer>
        <modules runAllManagedModulesForAllRequests="true"/>
        <!--
            To browse web app root directory during debugging, set the value below to true.
            Set to false before deployment to avoid disclosing web app folder information.
          -->
        <directoryBrowse enabled="true"/>
      </system.webServer>
    </configuration>

    Contracts:

    [ServiceContract]
        public interface IPromoCenyParseService
        {
            [OperationContract]
            Product ParseItem(string url);
    
            [OperationContract]
            List<Product> ParsePage(string urlPage);
    
            [OperationContract]
            int GetNumOfPages(string urlPage);
        }
    
        [DataContract]
        public class Product
        {
            [DataMember]
            public string ProductName
            {
                get;
                set;
            }
    
            [DataMember]
            public string Shop
            {
                get;
                set;
            }
    
            [DataMember]
            public string CategoryString
            {
                get;
                set;
            }
    
            [DataMember]
            public DateTime DiscountStart
            {
                get;
                set;
            }
    
            [DataMember]
            public DateTime DiscountEnd
            {
                get;
                set;
            }
    
            [DataMember]
            public string Note
            {
                get;
                set;
            }
    
            [DataMember]
            public double Price
            {
                get;
                set;
            }
    
            [DataMember]
            public double OldPrice
            {
                get;
                set;
            }
        }

    Impementation:

    [WebInvoke(Method = "GET",
                        ResponseFormat = WebMessageFormat.Json,
                        UriTemplate = "item?url={url}")]
            public Product ParseItem(string url)
    {
    }
    
    
    [WebInvoke(Method = "GET",
                        ResponseFormat = WebMessageFormat.Json,
                        UriTemplate = "item?url={url}")]
            public int GetNumOfPages(string urlPage)
    {
    }

    Note, that when I am invoking method GetNumOfPages, its returning correct result, but invoking ParseItem changes my URL to bad one

    Tuesday, March 18, 2014 3:36 PM

Answers

  • User-417640953 posted

    Hi TomaszBitowt,

    Thank you post the issue to our forum.

    The same issue has been solved in below thread.

    http://stackoverflow.com/questions/22482374/wcf-service-converts-localhost-to-www-localhost-com-only-in-case-of-json-respons

    The problem came from below data member.

      [DataMember]
            public DateTime DiscountStart
            {
                get;
                set;
            }
    
            [DataMember]
            public DateTime DiscountEnd
            {
                get;
                set;
            }
    

    As said, there is different Time Zone or different default date format on server. So right code should be like below.

    [DataMember]
    public DateTime DiscountStart
    {
        get { return this._discountStart.ToUniversalTime(); }
        set { this._discountStart = value; }
    }

    Thanks.

    Best Regards!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 20, 2014 10:20 PM