执行基于JSON的Restful WCF服务时出错

时间:2014-11-09 15:17:55

标签: json web-services wcf rest restsharp

我正在尝试编写基于json的restful WCF服务。当我使用RestSharp执行它时,我得到一个null响应,ErrorException和ErrorMessage中有以下错误。 根级别的数据无效。第1行,第1位。

状态代码是BadRequest

数据对象创建为

[DataContract]
public class User
{        
    [DataMember]
    public string first_name { get; set; }

    [DataMember]
    public string last_name { get; set; }

    [DataMember]
    public string email_address { get; set; }

    [DataMember]
    public List<UserAddress> Address; //do not serialize this in the data contract
}

[DataContract]
public class UserAddress
{
    [DataMember]
    public string line_1_address { get; set; }

    [DataMember]
    public string state { get; set; }

    [DataMember]
    public string PostalArea { get; set; }
}

服务代码是

   [ServiceContract]
    public interface IUserService
    {
        [OperationContract]
        User AddUser(User user);
    }


    public class UserService : IUserService
    {
        [WebInvoke(Method = "PUT",
           RequestFormat = WebMessageFormat.Json,
           ResponseFormat = WebMessageFormat.Json,
           UriTemplate = "User/")]
        public User AddUser(User user)
        {
            return new User();
        }
    }

该服务托管在Web应用程序中。

我有一个基于RestSharp的c#客户端

    static void Main(string[] args)
    {
        User u = new User();
        u.first_name = "testFirst";
        u.last_name = "testLast";

        UserAddress ua = new UserAddress();
        ua.line_1_address = "line 1";
        ua.PostalArea = "08648";
        ua.state = "NJ";

        u.Address = new List<UserAddress>();
        u.Address.Add(ua);

        RestSharp.RestClient client = new RestSharp.RestClient("http://localhost/TestWCHHost/User.svc");
        var request = new RestRequest(String.Format("User/"), Method.PUT);
        request.RequestFormat = DataFormat.Json;
        request.AddObject(u);
        var response = client.Execute<User>(request);
    }

网络配置更改为

<?xml version="1.0"?>
<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>

      <!-- User Service -->
      <service name="TestService.UserService" behaviorConfiguration="serviceBehavior">
        <endpoint address="" binding="webHttpBinding" contract="TestService.IUserService" behaviorConfiguration="web">
        </endpoint>
      </service>

    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="serviceBehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
  </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>

无法确定最新情况。任何线索。

0 个答案:

没有答案