C# 正在发送带有对象参数(.NET WCF服务)的PUT请求
所以,这是我的问题 我目前正在开发一个WCF REST服务,现在我需要实现一个允许我创建帐户(用户帐户)的方法。这次在网上搜索对我没有帮助,这就是为什么我要问一个真正了解我的意思的人(x) 所以,这是我的方法:C# 正在发送带有对象参数(.NET WCF服务)的PUT请求,c#,object,post,web,service,C#,Object,Post,Web,Service,所以,这是我的问题 我目前正在开发一个WCF REST服务,现在我需要实现一个允许我创建帐户(用户帐户)的方法。这次在网上搜索对我没有帮助,这就是为什么我要问一个真正了解我的意思的人(x) 所以,这是我的方法: [WebInvoke(Method = "PUT", UriTemplate = "users/{username}", RequestFormat = WebMessageFormat.Xml)] [OperationContract] void PutUse
[WebInvoke(Method = "PUT", UriTemplate = "users/{username}",
RequestFormat = WebMessageFormat.Xml)]
[OperationContract]
void PutUserAccount(string username, User u)
{
Debug.WriteLine("Entering method [PutUserAccount]");
// Check if user exists
User user = DBManager.FindUser(username);
if (user == null)
{
try
{
//Create or update new user
u.Id = GetUserLink(username);
u.Notes = GetUserNotesLink(username);
u.BlackListed = false;
DBManager.AddUser(u);
// Set status to created and include new URI in Location
//header
WebOperationContext.Current.OutgoingResponse.SetStatusAsCreated(GetUserProfile(username).Id);
}
catch (Exception e)
{
Debug.WriteLine("_PutUserAccount || Exception [" + e.Message + "]");
}
}
else if (!DBManager.IsUserAuthorized(username, WebOperationContext.Current))
{
Debug.WriteLine("_PutUserAccount - 1. This user isn't allowed to add users");
WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.Unauthorized;
return;
}
}
这是我的“用户”类:
问题是:我不能做任何填写“用户u”的请求,它总是返回以下异常:
Exception thrown: 'System.InvalidOperationException' in
System.ServiceModel.Web.dll
Exception thrown: 'System.InvalidOperationException' in
System.ServiceModel.dll
Exception thrown: 'System.InvalidOperationException' in
System.ServiceModel.dll
Exception thrown: 'System.InvalidOperationException' in
System.ServiceModel.dll
这是我的邮递员请求:
PUT请求:{RandomUser}
和我的XML正文:
<User>
<Id>something</Id>
<Username>RandomUser</Username>
<Password>1234</Password>
<Name>Random User</Name>
<Email>random.user@randomemail.com</Email>
<Notes>http://www.random-link.com</Notes>
<BlackListed>false</BlackListed>
</User>
某物
随机用户
1234
随机用户
随机的user@randomemail.com
http://www.random-link.com
假的
我的问题是,我必须如何构建请求才能将用户传递到我的服务
附言:我正在用邮递员来测试我的代码。好吧,经过很多努力,我终于做到了。我所要做的就是在“User xmlns”中添加对象的名称空间,并将数据按正确的顺序排列。这是我的工作要求:
<User xmlns="http://schemas.datacontract.org/2004/07/namespace.lib.Objects" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<BlackListed>false</BlackListed>
<Email>okabe.rintarou@achannel.com</Email>
<Id></Id>
<Name>Okabe Rintarou</Name>
<Notes></Notes>
<Password>1234</Password>
<Username>Kyouma</Username>
</User>
假的
冈贝。rintarou@achannel.com
冈倍仁太郎
1234
圭马
已经一个星期了,我一直在努力做这件事,我很傻
谢谢异常在哪里引发?
GetUserLink
和GetUserNotesLink
返回什么?您正在控制器中使用User
,但显示了\u User
类。你的邮递员请求和请求机构是什么样子的?@nilsK嗨,谢谢你的回复。在请求进入方法之前引发异常。我创建了一个临时类“Notes”作为string,因为它通常是一个URL(我认为这可能是问题所在,但事实并非如此)。(有关邮递员请求,请参见编辑)。
<User xmlns="http://schemas.datacontract.org/2004/07/namespace.lib.Objects" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<BlackListed>false</BlackListed>
<Email>okabe.rintarou@achannel.com</Email>
<Id></Id>
<Name>Okabe Rintarou</Name>
<Notes></Notes>
<Password>1234</Password>
<Username>Kyouma</Username>
</User>