我已经创建了WCF服务。我创建了两个非参数方法的常规方法。但是我创建了一个新方法,但这次需要两个参数。方法就像
接口
[OperationContract]
[WebInvoke(ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "GetClassCode/{name}/{password}")]
int GetAuthenticatedUserData(string name, string password);
.CS
public int GetAuthenticatedUserData(string name, string password)
{
try
{
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection(@"Data Source=HITSEDGE\HITSEDGE;Initial Catalog=CCIL1;User ID=dbassist;Password=assist@123"))
{
using (SqlCommand cmd = new SqlCommand("SELECT Name,Password from dbo.[CCIL$ADCS User] where Name=" + name + " and password" + password, con))
{
con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
if (dt != null)
{
return 1;
}
else
return 0;
}
}
}
catch (Exception)
{
throw;
}
}
我的web.config就像
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<bindings>
<!--<basicHttpBinding>
<binding name="NewBinding0" />
</basicHttpBinding>-->
<basicHttpBinding>
<binding name="ExtendedMaxSize"
maxBufferSize="999999" maxReceivedMessageSize="999999" />
</basicHttpBinding>
<webHttpBinding>
<binding name="crossDomain" crossDomainScriptAccessEnabled="true"></binding>
</webHttpBinding>
</bindings>
<services>
<!--<service name="MyService.MyService">
<endpoint address="mex" binding="basicHttpBinding"
bindingConfiguration="NewBinding0" contract="MyService.IMyService" />
</service>-->
<service name="MyService.MyService" behaviorConfiguration="DefaultBehavior">
<endpoint address="" binding="webHttpBinding" contract="MyService.IMyService" name="MyService.MyService" behaviorConfiguration="AjaxBehavior" bindingConfiguration="crossDomain">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="DefaultBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="AjaxBehavior">
<webHttp></webHttp>
<!--<webHttp helpEnabled="true" />-->
</behavior>
</endpointBehaviors>
<!--<serviceBehaviors>
<behavior>
-->
<!-- To avoid disclosing metadata information, set the value below to false before deployment -->
<!--
<serviceMetadata httpGetEnabled="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>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<!--
To browse web app root directory during debugging, set the value below to true.
Set to false before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
但是当我通过URL调用我的参数方法时,如下面的url我得到错误找不到端点。
网址
本地主机/ MyService.svc / GetAuthenticatedUserData名称=&#39;?Himansu&#39;&安培;密码=&#39;命中@ 123&#39;
请帮我创建终点...
答案 0 :(得分:0)
原因是您在方法上指定了以下属性:
UriTemplate = "GetClassCode/{name}/{password}"
这意味着您的终端URL实际上是:
http://localhost/MyService.svc/GetClassCode/Himansu/hits@123
最好不要将用户名和密码作为URL的一部分,并将这些作为查询字符串参数保留。所以,你可以拥有以下内容:
UriTemplate = "GetAuthenticatedUserData"
您的用户名和密码将以相同的方式传递,使用查询字符串参数。