是的,这是可能的。
a)将 xml 更改为
<Login>
<username>test</username>
<password>foo</password>
</Login>
b)替换 [ServiceContract]
为[ServiceContract(Namespace = "")]
c)并将方法声明为
[OperationContract]
[WebInvoke(RequestFormat = WebMessageFormat.Xml,
ResponseFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Wrapped)]
public string Login(string username, string password){}
它有效....
这是我用来测试的代码
public void TestWCFService()
{
//Start Server
Task.Factory.StartNew(
(_) =>
{
Uri baseAddress = new Uri("http://localhost:8080/Test");
WebServiceHost host = new WebServiceHost(typeof(TestService),
baseAddress);
host.Open();
}, null, TaskCreationOptions.LongRunning).Wait();
//Client
string xml = @"<Login>
<username>test</username>
<password>foo</password>
</Login>";
var wc = new WebClient();
wc.Headers.Add("Content-Type", "application/xml; charset=utf-8");
var result = wc.UploadString("http://localhost:8080/Test/Login", xml);
}
[ServiceContract(Namespace = "")]
public class TestService
{
[OperationContract]
[WebInvoke(RequestFormat = WebMessageFormat.Xml,
ResponseFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Wrapped)]
public string Login(string username, string password)
{
return username + " " + password;
}
}