好的!这是 GET 方法,虽然强烈不建议在查询字符串中发送用户名和密码,但为了让您可以使用它。
将您的合同定义为:
[ServiceContract]
public interface IMyService
{
[OperationContract]
[WebGet(UriTemplate = "/username={username}&password={password}",
ResponseFormat = WebMessageFormat.XML,
BodyStyle = WebMessageBodyStyle.Bare)]
string MyMethod(string username, string password);
}
并且在您的实现方法中,您可以检索相同的用户名和密码。
public class MyServiceImpl:IMyService
{
public string MyMethod(string username, string password)
{
return "You entered "+username +" and "+password; // This will returned as an XML.
}
}
改变你的 Web.Config 像:
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" maxRequestLength="2147483647" executionTimeout="300"/>
</system.web>
<system.serviceModel>
<services>
<service behaviorConfiguration="" name="Namespace.MyService">
<endpoint address="" behaviorConfiguration="RESTBehavior" binding="webHttpBinding"
bindingConfiguration="webHttpBG" contract="Namespace.Name.IMyService" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:51855/MyService.svc" />
</baseAddresses>
</host>
</service>
</services>
<bindings>
<webHttpBinding>
<binding name="webHttpBG" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647"
maxBufferPoolSize="21474836470" closeTimeout="00:01:00" openTimeout="00:01:00"
receiveTimeout="00:10:00" sendTimeout="00:01:00">
<security mode="None"/>
</binding>
</webHttpBinding>
</bindings>
<behaviors>
<endpointBehaviors>
<behavior name="RESTBehavior">
<webHttp helpEnabled="true"/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="webHttpBinding" scheme="http" bindingConfiguration="webHttpBG" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
对于测试,您可以使用浏览器。键入在 Web.Config 文件中定义的 url。例如
http://localhost:51855/MyService.svc/username=myname&password=123
当您在浏览器上输入此网址并按 Enter 时,如果服务正在运行,它将调用您的“MyMethod”。
您也可以使用 Fiddler 来测试您的服务。