我通过控制台应用程序创建Restful服务主机,app.config和界面如下所示:
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
<client/>
<bindings>
<webHttpBinding>
<binding name="MyAppServiceBinding" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" transferMode="Buffered">
<readerQuotas maxDepth="64" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>
</binding>
</webHttpBinding>
</bindings>
<standardEndpoints/>
<services>
<service behaviorConfiguration="MyAppServiceBehavior" name="MyAppService">
<endpoint behaviorConfiguration="MyAppServiceEndPointBehavior" binding="webHttpBinding" bindingConfiguration="MyAppServiceBinding" name="MyAppServiceRESTEndPoint" contract="IMyAppService" kind="" endpointConfiguration=""/>
<endpoint address="mex" binding="mexHttpBinding" bindingConfiguration="" name="MyAppServiceMex" contract="IMetadataExchange"/>
<host>
<baseAddresses>
<add baseAddress="http://localhost:51920/MyAppService/"/>
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="MyAppServiceEndPointBehavior">
<webHttp helpEnabled="true" defaultOutgoingResponseFormat="Json"
automaticFormatSelectionEnabled="false" />
<dataContractSerializer />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="MyAppServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug />
<dataContractSerializer />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
[ServiceContract]
public interface IMyAppService
{
/// <summary>
/// Set Device Configuration
/// </summary>
/// <param name="setDeviceConfiguration"></param>
/// <returns></returns>
[OperationContract]
[WebInvoke(Method = "POST",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "SetDeviceConfiguration")]
SetDeviceConfigurationResponse SetDeviceConfiguration(SetDeviceConfiguration setDeviceConfiguration);
}
然后创建一个访问控制台,但得到Bad Request 400。 RestClient restClient = new RestClient(ServiceUri);
RestRequest restRequest = new RestRequest(typeof(T).Name, Method.POST);
restRequest.RequestFormat = DataFormat.Json;
restRequest.AddBody(request);
string json = JsonConvert.SerializeObject(request);
int nLen1 = json.Length;
int nLen2 = Encoding.UTF8.GetBytes(json).Length;
IRestResponse<R> response = restClient.Execute<R>(restRequest);
if (response.StatusCode != HttpStatusCode.OK)
{
throw response.ErrorException;
}
SetDeviceConfiguration是一个DataContract,如果我新建这个没有设置的对象,就会调用该服务,但填充数据差不多55kb总是得到Bad Request。如果我将Interface更改为this,那就可以了。
[OperationContract]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "SetDeviceConfiguration")]
SetDeviceConfigurationResponse SetDeviceConfiguration(Stream stream);
有人可以帮忙吗?提前谢谢。