要开始使用WCF,最简单的方法是使用默认的SOAP格式和HTTP POST(而不是GET)来进行Web服务绑定。最简单的HTTP绑定是“basicHttpBinding”。以下是ServiceContract / OperationContract对您的登录服务的外观示例:
[ServiceContract(Namespace="http://mycompany.com/LoginService")]
public interface ILoginService
{
[OperationContract]
string Login(string username, string password);
}
服务的实现可能如下所示:
public class LoginService : ILoginService
{
public string Login(string username, string password)
{
// Do something with username, password to get/create sessionId
// string sessionId = "12345678";
string sessionId = OperationContext.Current.SessionId;
return sessionId;
}
}
您可以使用ServiceHost将其作为Windows服务托管,也可以像在普通的ASP.NET Web(服务)应用程序中一样在IIS中托管它。这两个教程都有很多教程。
WCF服务配置可能如下所示:
behaviorConfiguration="LoginServiceBehavior" >
address=""
binding="basicHttpBinding"
contract="WcfTest.ILoginService" />
address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange" />
(MEX的东西对于生产是可选的,但是需要使用WcfTestClient.exe进行测试,以及用于公开服务元数据)。
您必须修改Java代码以将SOAP消息POST到服务。在与非WCF客户端进行交互操作时,WCF可能会有点挑剔,因此您必须稍微弄乱POST头文件才能使其工作。一旦运行,您就可以开始研究登录的安全性(可能需要使用不同的绑定来获得更好的安全性),或者可能使用WCF REST来允许使用GET而不是SOAP / POST进行登录。
以下是HTTP代码应该从Java代码中看起来的示例。有一个名为“ Fiddler ” 的工具,对于调试Web服务非常有用。
POST /LoginService HTTP/1.1
Content-Type: text/xml; charset=utf-8
SOAPAction: "http://mycompany.com/LoginService/ILoginService/Login"
Host: somesite.com:55555
Content-Length: 216
Expect: 100-continue
Connection: Keep-Alive
Blah
Blah2