1

I am using Newtonsoft.Json dll for Serialize object to return string from wcf service. When I call other simple method from service it works fine but when I call method which serialize object into string and return that string its not working. Below is my code of service.

public string GetString()
    {
        return "Hello";
    }

    public string GetData(int i)
    {
        My_Entities ME = new MY_Entities();

        ApplicationVM oVM = new ApplicationVM()
        {
            AP_M_BloodGroup = ME.AP_M_BloodGroup.ToList().ElementAtOrDefault(i),
            AP_M_DayMaster = ME.AP_M_DayMaster.ToList().ElementAtOrDefault(i)
        };
        return JsonConvert.SerializeObject(oVM, Formatting.Indented);
    }

Both method work for local but after hosting when calling GetData() method it will give error message "Access Denied" at client side. So, what is the problem and what I need to put extra?

When I put client side code in try-catch block it gives message like:

Message: Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed

Mehul Patel
  • 1,084
  • 2
  • 12
  • 28
  • 1
    what is your class `ApplicationVM` doing? is it accessing some resources? what identity is you application pool running under? – Andrew Apr 18 '14 at 11:50
  • ApplicationVM is simple Entity model class which consume two entity class for BloodGroup and DayMaster. – Mehul Patel Apr 18 '14 at 12:02
  • Are you able to create context class within `GetData` method? What credentials are you using to connect to your database? first method simply returns an constant - that has nothing to say. – Andrew Apr 18 '14 at 12:04
  • yes, it works proper and also work fine with inbuilt js serialization. – Mehul Patel Apr 18 '14 at 12:12
  • 2
    Will this help? under system.web in web.config – d_z Apr 18 '14 at 12:14
  • BTW, where is WCF service hosted? IIS? – d_z Apr 18 '14 at 12:15
  • In IIS working fine but after hosting on web server this problem started. – Mehul Patel Apr 18 '14 at 12:21
  • Than it might be a problem with trust level. Here is a problem looking like yours [link](http://social.msdn.microsoft.com/Forums/vstudio/en-US/e415255c-0fd3-4500-8e46-4d36220913fe/systemsecuritysecurityexception-request-for-the-permission-of-type?forum=wcf) – d_z Apr 18 '14 at 12:26
  • Thanks @d_z it will work. I miss that in my web.config file. – Mehul Patel Apr 18 '14 at 12:31
  • possible duplicate of [How can I return json from my WCF rest service (.NET 4), using Json.Net, without it being a string, wrapped in quotes?](http://stackoverflow.com/questions/3026934/how-can-i-return-json-from-my-wcf-rest-service-net-4-using-json-net-without) – Wim Ombelets Apr 18 '14 at 12:32

1 Answers1

1

I use

<trust level="Full" />

under system.web in web.config as per @d_Z suggested and it works.

Mehul Patel
  • 1,084
  • 2
  • 12
  • 28