我为插入,更新,删除和检索数据创建了一个WCF服务。在IService1.cs文件中,我有以下代码。我在哪里宣布五种方法。
public interface IService1
{
[OperationContract]
[WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped, Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "getInfo")]
CustomerData Get();
[OperationContract]
[WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped, Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "GetJson")]
String GetJson();
[OperationContract]
[WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped, Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "Insert")]
void Insert(string name, string country);
[OperationContract]
[WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped, Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "Update")]
void Update(int customerId, string name, string country);
[OperationContract]
[WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped, Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "Delete")]
void Delete(int customerId);
}
[DataContract]
public class CustomerData
{
public CustomerData()
{
this.CustomersTable = new DataTable("CustomersData");
}
[DataMember]
public DataTable CustomersTable { get; set; }
}
我刚刚创建了一个名为" GetJson"的方法。从数据库表中获取数据并将其转换为Json strig并返回它。
在Service1.svc.cs文件中,我有以下代码。
public class Service1 : IService1
{
public CustomerData Get()
{
String CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
using (SqlCommand cmd = new SqlCommand("select * from tblCustomers", con))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
cmd.Connection = con;
using (DataTable dt = new DataTable())
{
CustomerData customer = new CustomerData();
da.Fill(customer.CustomersTable);
return customer;
}
}
}
}
}
public String GetJson()
{
String CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
using (SqlCommand cmd = new SqlCommand("select * from tblCustomers", con))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
cmd.Connection = con;
using (DataTable dt = new DataTable())
{
CustomerData customer = new CustomerData();
da.Fill(customer.CustomersTable);
String JsonString = JsonConvert.SerializeObject(customer.CustomersTable);
return JsonString;
}
}
}
}
}
public void Insert(String name, String country)
{
String CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
con.Open();
SqlCommand cmd = new SqlCommand("Insert into tblCustomers(Name, Country) values(@name,@country)", con);
cmd.Parameters.AddWithValue("@name", name);
cmd.Parameters.AddWithValue("@country", country);
cmd.ExecuteNonQuery();
}
}
public void Update(int customerId, string name, string country)
{
string constr = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("UPDATE tblCustomers SET Name = @Name, Country = @Country WHERE CustomerId = @CustomerId"))
{
cmd.Parameters.AddWithValue("@CustomerId", customerId);
cmd.Parameters.AddWithValue("@Name", name);
cmd.Parameters.AddWithValue("@Country", country);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
public void Delete(int customerId)
{
string constr = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("DELETE FROM tblCustomers WHERE CustomerId = @CustomerId"))
{
cmd.Parameters.AddWithValue("@CustomerId", customerId);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
}
在" GetJson"方法datatable转换为Json String。
在代码之后的Web.config文件中String JsonString = JsonConvert.SerializeObject(customer.CustomersTable);
返回JsonString;
<configuration> <connectionStrings>
<add name="DBCS" connectionString="data source=.\MSSQL; database=Customers; integrated security=SSPI"/> </connectionStrings> <system.web>
<compilation debug="true" targetFramework="4.0" /> </system.web> <system.serviceModel>
<services>
<service name="WcfService2.Service1" behaviorConfiguration="WcfService2.Service1Behavior">
<endpoint address="" binding="webHttpBinding" behaviorConfiguration="REST" contract="WcfService2.IService1"></endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="WcfService2.Service1Behavior">
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above 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>
<endpointBehaviors>
<behavior name="REST">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> </system.serviceModel> <system.webServer>
<modules runAllManagedModulesForAllRequests="true"/> </system.webServer> </configuration>
此Web服务已在IIS 7.5上成功托管,但在我尝试运行时
显示错误&#34;未找到端点&#34;。我认为Web.config文件中存在问题
<system.serviceModel>
<services>
<service name="WcfService2.Service1" behaviorConfiguration="WcfService2.Service1Behavior">
<endpoint address="" binding="webHttpBinding" behaviorConfiguration="REST" contract="WcfService2.IService1"></endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="WcfService2.Service1Behavior">
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above 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>
<endpointBehaviors>
<behavior name="REST">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
答案 0 :(得分:0)
您在behaviorConfiguration
代码中有service
个属性重复。
尝试删除behaviorConfiguration="WcfService2.Service1Behavior"
。