locked
Json deserialization over a WCF service RRS feed

  • Question

  • Hi all,I've written a little test app for Windows Phone 8 that connects to a WCF service and requests some user data. The user data is stored in a server-side json file in an arbitrary location -

    "C:\Users\Me\Documents\Person.json"

    The WCF method and classes are set-up like so:

    public AllUsersClass GetUsers(string path)
            {
                
    
                var json = File.ReadAllText(@"C:\Users\Me\Documents\Person.json");
                var users = (AllUsersClass)JsonConvert.DeserializeObject(json, typeof(AllUsersClass));
    
                
    
                return users;
            }
    
    [DataContract]
        public class User
        {
            /// <summary>
            /// Must be a unique username
            /// </summary>
            [DataMember]
            private string _username;
            [DataMember]
            public string Username
            {
                get { return _username; }
                set { _username = value; }
            }
            [DataMember]
            private List<string> _friends = new List<string>();
            [DataMember]
            public List<string> Friends
            {
                get { return _friends; }
                set { _friends = value; }
            }
        }
    
        [KnownType(typeof(User))]
        //[KnownType(typeof(List<User>))]
        [DataContract]
        public class AllUsersClass
        {
            [DataMember]
            public List<User> AllUsers { get; set; }
        }
    I should also say that I'm using Newtonsoft.Json and have had the exact same method deserializing the exact same json file when all done locally and not over a service.

    I initially had some permission problems so I tried changing application pool settings in IIS (not Express) and I amended the web.config to turn security off which  meant that the client call runs successfully now:

    <configuration>
      
      <appSettings>
        <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
      </appSettings>
      <system.web>
        <compilation debug="true" targetFramework="4.5" />
        <httpRuntime targetFramework="4.5"/>
      </system.web>
      <system.serviceModel>
        <services>
          <service name="LinkUpService.Service1" behaviorConfiguration="behaviourConfig">
            <endpoint address="" binding="basicHttpBinding" bindingConfiguration="httpBinding" contract="LinkUpService.ILinkUpService" />
          </service>
        </services>
        <diagnostics>
          <endToEndTracing activityTracing="true" messageFlowTracing="true" propagateActivity="true" />
        </diagnostics>
        <bindings>
          <basicHttpBinding>
            <binding name ="httpBinding">
              <security mode="None">
                <transport clientCredentialType="Basic" />
              </security>
            </binding>
          </basicHttpBinding>
            
          
        </bindings>
        <behaviors>
          <serviceBehaviors>
            <behavior name="behaviourConfig">
              <!-- 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"/>
            </behavior>
          </serviceBehaviors>
        </behaviors>
        <protocolMapping>
            <add binding="basicHttpBinding" scheme="https" />
        </protocolMapping>    
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
      </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>

    I'm sorry for the verbosity of this post too, but I don't want to miss anything (I probably will). The problem is that the GetUsers() method returns nothing, I believe it's something to do with the clients' access rights on the servers file system, but I've tried a few things now such as the applicationpool amendment to give the service higher privs via IIS.

    Am I doing anything obviously stupid here? Any pointers would be great.

    Kind regards,

    Jib






    Wednesday, July 10, 2013 7:31 PM

Answers

  • hi! Have you tried creating a new file within the solution explorer with same contents as your json file? That way your client has access to a file that is actually in your control. Keep the new file's build action as resource in its properties and that should yield what you are expecting.
    • Marked as answer by Jibrohni Thursday, July 11, 2013 8:44 AM
    Wednesday, July 10, 2013 8:24 PM

All replies

  • hi! Have you tried creating a new file within the solution explorer with same contents as your json file? That way your client has access to a file that is actually in your control. Keep the new file's build action as resource in its properties and that should yield what you are expecting.
    • Marked as answer by Jibrohni Thursday, July 11, 2013 8:44 AM
    Wednesday, July 10, 2013 8:24 PM
  • Ah, that makes a LOT more sense! Thanks.

    Out of interest though say I was accessing a database on the server - would that also need to be included as part of the service solution?
    Thursday, July 11, 2013 8:44 AM