我有一个WCF REST服务正在恢复(POST)一串base64编码的json数据。当我在fiddler2中测试时,我得到400 bad request error
问题似乎与发布的字符串的长度有关但我在网络配置中没有得到它我将缓冲区和内容设置为应该是的值。我不知道mybe the fiddlers缓冲区不够,但这个奇怪我想
在我的测试中,我发现如果请求比实际的完整编码对象短一点,服务实际上会激活所以我猜它与缓冲区有关但我没有看到问题。
我从服务中获取了编码对象作为get并尝试将其放回,以便正确编码对象
希望有人可以帮助找到解决方案,因为我认为这是我错过的细节。
提琴手电话:
http://Server/WcfSyncDBService/SyncDBService.svc/AddTodoItems/PEFycmF5T2ZJdGVtcyB4bWxucz0iaHR0cDovL3NjaGVtYXMuZGF0YWNvbnRyYWN0Lm9yZy8yMDA0LzA3L1djZlN5bmNEQlNlcnZpY2UiIHhtbG5zOmk9Imh0dHA6Ly93d3cudzMub3JnLzIwMDEvWE1MU2NoZW1hLWluc3RhbmNlIj48SXRlbXM+PENhdGVnb3J5PlJlbWluZGVyPC9DYXRlZ29yeT48RGVzY3JpcHRpb24+UmVtaW5kZXI8L0Rlc2NyaXB0aW9uPjxJZD41PC9JZD48U3VtbWFyeT5UZXN0IDU8L1N1bW1hcnk+PC9JdGVtcz48L0FycmF5T2ZJdGVtcz4=
这是我的代码:
接口:
[OperationContract(Name = "AddTodoItems")]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare,
ResponseFormat = WebMessageFormat.Json, UriTemplate = "AddTodoItems/{content}")]
string AddTodoItems(string content);
实现:
public string AddTodoItems(string content)
{
var ret = String.Empty;
try
{
var data = DecodeFrom64(content);
ret = "Encoded:<" + content + ">";
ret = ret + "content:<" + data+">";
var ms = new MemoryStream(Encoding.UTF8.GetBytes(data));
var serializer = new DataContractSerializer(typeof(TodoItem[]));
var todoArray = (TodoItem[])serializer.ReadObject(ms);
var todoList = todoArray.ToList();
if (todoList.Count > 0)
{
var context = new SyncDBEntities();
foreach (var item in todoList)
{
var dbItem = (from i in context.todo where i.C_id == item.Id select i).First();
if (dbItem == null ) // insert
{
var itemAdd = new SyncDBmodel.todo();
itemAdd.C_id = item.Id;
itemAdd.category = item.Category;
itemAdd.summary = item.Summary;
itemAdd.description = item.Description;
context.AddTotodo(itemAdd);
context.SaveChanges();
}
else // update
{
dbItem.C_id = item.Id;
dbItem.category = item.Category;
dbItem.summary = item.Summary;
dbItem.description = item.Description;
context.SaveChanges();
}
}
}
}
catch (Exception e)
{
ret = ret+"Error:<"+e.Message+">";
ret = ret + "InnerException:<" + e.InnerException + ">";
}
finally
{
if (String.IsNullOrEmpty(ret))
ret = "OK";
}
return ret;
}
public bool DelTodoItems(string content)
{
bool ret = true;
try
{
var ms = new MemoryStream(Encoding.UTF8.GetBytes(content));
var serializer = new DataContractSerializer(typeof(TodoItem[]));
var todoArray = (TodoItem[])serializer.ReadObject(ms);
var todoList = todoArray.ToList();
if (todoList.Count > 0)
{
var context = new SyncDBEntities();
foreach (var item in todoList)
{
var dbItem = (from i in context.todo where i.C_id == item.Id select i).First();
if (dbItem != null) // delete
{
context.DeleteObject(dbItem);
context.SaveChanges();
}
}
}
}
catch (Exception)
{
ret = false;
}
return ret;
}
的Web.config:
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<httpRuntime maxRequestLength="524288" />
</system.web>
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding name="StreamedRequestWebBinding"
bypassProxyOnLocal="true"
useDefaultWebProxy="false"
hostNameComparisonMode="WeakWildcard"
sendTimeout="10:15:00"
openTimeout="10:15:00"
receiveTimeout="10:15:00"
maxReceivedMessageSize="2147483647"
maxBufferSize="2147483647"
maxBufferPoolSize="2147483647"
transferMode="StreamedRequest">
<readerQuotas maxDepth="2147483647"
maxArrayLength="2147483647"
maxStringContentLength="2147483647"
maxBytesPerRead="2147483647"/>
<security mode="None"/>
</binding>
</webHttpBinding>
</bindings>
<services>
<service name="WcfSyncDBService.SyncDBService" behaviorConfiguration="ServiceBehaviour">
<endpoint
address =""
binding="webHttpBinding"
bindingConfiguration="StreamedRequestWebBinding"
contract="WcfSyncDBService.ISyncDBService"
behaviorConfiguration="web">
</endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehaviour">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<httpCompression directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files">
<scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll"/>
<dynamicTypes>
<add mimeType="text/*" enabled="true"/>
<add mimeType="application/xml" enabled="true" />
<add mimeType="application/json" enabled="true" />
<add mimeType="message/*" enabled="true"/>
<add mimeType="application/javascript" enabled="true"/>
<add mimeType="*/*" enabled="false"/>
</dynamicTypes>
<staticTypes>
<add mimeType="text/*" enabled="true"/>
<add mimeType="message/*" enabled="true"/>
<add mimeType="application/javascript" enabled="true"/>
<add mimeType="*/*" enabled="false"/>
</staticTypes>
</httpCompression>
</system.webServer>
<connectionStrings>
<add name="SyncDBEntities" connectionString="metadata=res://*/SyncDBmodel.csdl|res://*/SyncDBmodel.ssdl|res://*/SyncDBmodel.msl;provider=System.Data.SqlClient;provider connection string="data source=.;initial catalog=SyncDB;Persist Security Info=True;User ID=sa;Password=isql;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>