您好我是wcf服务的新手。我正在创建一个wcf示例程序。但是在浏览器中没有显示响应。我想在浏览器中回复显示。
下面是我的示例代码:
在界面中:
public interface ICitiesService
{
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate = "getdata", BodyStyle = WebMessageBodyStyle.Bare)]
// [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "getdata")]
string getdata();
}
课堂上的:
public class CitiesService : ICitiesService
{
public string getdata()
{
try
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con2"].ConnectionString);
SqlCommand comm = new SqlCommand("select CircleID, CircleName from Circle", con);
con.Open();
comm.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter(comm);
// DataSet ds = new DataSet();
DataTable dt = new DataTable();
da.Fill(dt);
JavaScriptSerializer JSSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
List<Dictionary<string, object>> DtRows = new List<Dictionary<string, object>>();
Dictionary<string, object> newrow = null;
//Code to loop each row in the datatable and add it to the dictionary object
foreach (DataRow drow in dt.Rows)
{
newrow = new Dictionary<string, object>();
foreach (DataColumn col in dt.Columns)
{
newrow.Add(col.ColumnName.Trim(), drow[col]);
}
DtRows.Add(newrow);
}
con.Close();
return JSSerializer.Serialize(DtRows);
}
catch (Exception ex)
{
List<String> Parameters = new List<String>();
Parameters.Add("getRechargeCircleList");
// SendErrorMail(ex.ToString(), "getRechargeCircleList", Parameters);
// Context.Response.ContentType = "application/json; charset=utf-8";
// Context.Response.Write(@" {""Status"":""false"",""Message"" : ""Oops! Something went Wrong""}");
// return;
}
return "false";
}
}
web.config文件:
<?xml version="1.0"?>
<configuration>
<connectionStrings>
</connectionStrings>
<appSettings/>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
<!--
The <authentication> section enables configuration
of the security authentication mode used by
ASP.NET to identify an incoming user.
-->
<authentication mode="Windows"/>
<!--
The <customErrors> section enables configuration
of what to do if/when an unhandled error occurs
during the execution of a request. Specifically,
it enables developers to configure html error pages
to be displayed in place of a error stack trace.
<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
-->
<pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/>
</system.web>
<!--
The system.webServer section is required for running ASP.NET AJAX under Internet
Information Services 7.0. It is not necessary for previous version of IIS.
-->
<system.webServer>
<!--
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>
<system.serviceModel>
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint name="web" helpEnabled="true" automaticFormatSelectionEnabled="true" />
</webHttpEndpoint>
</standardEndpoints>
<services>
<service behaviorConfiguration="PavanWcfservice.Service1Behavior" name="PavanWcfservice.Service1">
<endpoint address="" binding="basicHttpBinding" contract="PavanWcfservice.IService1">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="" binding="netTcpBinding" contract="PavanWcfservice.IService1"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
<service behaviorConfiguration="PavanWcfservice.CitiesBehavior" name="PavanWcfservice.Cities">
<endpoint address="" binding="basicHttpBinding" contract="PavanWcfservice.ICities">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
<service behaviorConfiguration="PavanWcfservice.CitiesServiceBehavior" name="PavanWcfservice.CitiesService">
<endpoint address="" binding="basicHttpBinding" contract="PavanWcfservice.ICitiesService">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="PavanWcfservice.Service1Behavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
<behavior name="PavanWcfservice.CitiesBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
<behavior name="PavanWcfservice.CitiesServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
web.config文件中是否有任何更改。我不知道,请帮助我..我想将结果显示在浏览器中。
每当我运行下面的服务是我的网址 &#34; http://localhost:/CitiesService.svc/getdata&#34;
空白页面会来。我应该怎么做....请帮帮我