【发布时间】:2014-01-06 05:56:37
【问题描述】:
将自定义对象传递给 REST WCF 操作会给我“错误请求错误”。我在这里尝试了 uri 路径和查询字符串类型方法。非常感谢任何帮助。
服务端代码
[ServiceContract]
public interface IRestService
{
[OperationContract]
[WebInvoke(UriTemplate = "getbook?tc={tc}",Method="POST",BodyStyle=WebMessageBodyStyle.Wrapped,RequestFormat=WebMessageFormat.Json)]
string GetBook(myclass mc);
}
[DataContract]
[KnownType(typeof(myclass))]
public class myclass
{
[DataMember]
public string name { get; set; }
}
public string GetBookById(myclass mc)
{
return mc.name;
}
客户端代码:
public static void GetString()
{
myclass mc = new myclass();
mc.name = "demo";
string jsn = new JavaScriptSerializer().Serialize(mc);
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(string.Format(@"http://localhost:55218/RestService.svc/getbook?mc={0}",jsn));
string svcCredentials = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes("bda11d91-7ere-4da1-2e5d-24adfe39d174"));
req.Headers.Add("Authorization", "Basic " + svcCredentials);
req.MaximumResponseHeadersLength = 2147483647;
req.Method = "POST";
req.ContentType = "application/json";
// exception is thrown here
using (WebResponse svcResponse = (HttpWebResponse)req.GetResponse())
{
using (StreamReader sr = new StreamReader(svcResponse.GetResponseStream()))
{
JavaScriptSerializer js = new JavaScriptSerializer();
string jsonTxt = sr.ReadToEnd();
}
}
}
服务配置
<basicHttpBinding>
<binding name="BasicHttpBinding_IService" closeTimeout="01:01:00"
openTimeout="01:01:00" receiveTimeout="01:10:00" sendTimeout="01:01:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"
messageEncoding="Text" textEncoding="utf-8" transferMode="Streamed"
useDefaultWebProxy="true">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647"
maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default"/>
</security>
</binding>
</basicHttpBinding>
<webHttpBinding>
<binding name="" closeTimeout="01:01:00" openTimeout="01:01:00"
receiveTimeout="01:10:00" sendTimeout="01:01:00" allowCookies="false"
bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"
transferMode="Streamed" useDefaultWebProxy="true">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
<security mode="None" />
</binding>
</webHttpBinding>
</bindings>
<services>
<service name="WcfRestSample.RestService" behaviorConfiguration="ServiceBehavior">
<endpoint address="" behaviorConfiguration="restfulBehavior"
binding="webHttpBinding" bindingConfiguration="" contract="WcfRestSample.IRestService" />
<!--<host>
<baseAddresses>
<add baseAddress="http://localhost/restservice" />
</baseAddresses>
</host>-->
</service>
</services>
<!-- behaviors settings -->
<behaviors>
<!-- end point behavior-->
<endpointBehaviors>
<behavior name="restfulBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceAuthorization serviceAuthorizationManagerType="WcfRestSample.APIKeyAuthorization, WcfRestSample" />
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint
automaticFormatSelectionEnabled="true"
helpEnabled="true" />
</webHttpEndpoint>
</standardEndpoints>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
</system.webServer>
【问题讨论】:
-
您正在使用包装的 bodystyle 属性,但我假设请求未相应包装。能不能把bodystyle属性值改成Bare试试。另外请使用 Fiddler 检查原始请求并在此处发布请求以帮助您
-
请同时发布您的服务配置文件。
-
我已经更新了我的问题,其中也包括服务配置。在这里,在这段代码中,我试图调用另一个我已经设置了 basichttpbinding 配置的服务(即 service.svc)。调用第二个服务也会抛出错误的请求错误。我不知道我哪里错了。
-
合约“IRestService”中的操作“getbook”有一个名为“tc”的“WcfRestSample.newclass”类型的查询变量,但“WcfRestSample.newclass”类型不能被“QueryStringConverter”转换。 UriTemplate 查询值的变量必须具有可由“QueryStringConverter”转换的类型。这是从提琴手捕获的原始数据。
-
当我将类类型参数或其他参数与流一起添加时,我的服务不允许我在浏览器中查看。它显示错误,使用流时不能有多个参数,如果我使用类参数,它显示查询字符串参数必须是字符串。因此,我发现很难调试服务,因为我的主要服务正在调用另一个辅助服务。如何克服这个问题或抑制这个问题??
标签: wcf rest c#-4.0 wcf-binding wcf-security