【发布时间】:2012-09-19 06:57:14
【问题描述】:
l - 这是我的 WCFSeviceLibrary 接口类
在这个接口类IService1中将OperationContract声明为一个ObjectClass类型的方法。
并为 Set Type 创建此 ObjectClass 类。作为 Ganeric。
public interface IService1
{
[OperationContract]
ObjectClass SetDataUsingDataContract(ObjectClass data);
}
[DataContract]
public class ObjectClass
{
string name;
string address;
string emailid;
double contactno;
[DataMember]
public string Name
{
set { name = value; }
get { return name; }
}
[DataMember]
public string Address
{
set { address = value; }
get { return address; }
}
[DataMember]
public string EmailId
{
set { emailid = value; }
get { return emailid; }
}
[DataMember]
public double ContactNO
{
set { contactno = value; }
get { return contactno; }
}
}
Service.cs 文件 在这个类中暗示
public class Service1 : IService1
{
public ObjectClass SetDataUsingDataContract(ObjectClass data)
{
SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\Pavan\WCF_Practice\WcfServiceSample\WebApplicationvc\App_Data\WCFDB.mdf;Integrated Security=True;User Instance=True");
conn.Open();
SqlCommand cmd = new SqlCommand("INSERT INTO WCFTBL (Name, Address, ContactNo, EmailID) VALUES ('"+data.Name+"','"+data.Address+"','"+data.ContactNO+"','"+data.EmailId+"')",conn);
cmd.ExecuteNonQuery();
conn.Close();
return data;
}
}
这是我的 Btn_Click 类 WebApplication 页面
protected void Add_Click(object sender, EventArgs e)
{
ServiceReference1.Service1Client srv = new ServiceReference1.Service1Client();
srv.SetDataUsingDataContract();
}
我不知道如何设置 srv.SetDataUsingDataContract() 方法的参数。
AppConfig 文件
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<compilation debug="true" />
</system.web>
<!-- When deploying the service library project, the content of the config file must be added to the host's
app.config file. System.Configuration does not support config files for libraries. -->
<system.serviceModel>
<services>
<service name="WcfServiceLibraryforADD.Service1" behaviorConfiguration="WcfServiceLibraryforADD.Service1Behavior">
<host>
<baseAddresses>
<add baseAddress = "http://localhost:8731/Design_Time_Addresses/WcfServiceLibraryforADD/Service1/" />
</baseAddresses>
</host>
<!-- Service Endpoints -->
<!-- Unless fully qualified, address is relative to base address supplied above -->
<endpoint address ="" binding="wsHttpBinding" contract="WcfServiceLibraryforADD.IService1">
<!--
Upon deployment, the following identity element should be removed or replaced to reflect the
identity under which the deployed service runs. If removed, WCF will infer an appropriate identity
automatically.
-->
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<!-- Metadata Endpoints -->
<!-- The Metadata Exchange endpoint is used by the service to describe itself to clients. -->
<!-- This endpoint does not use a secure binding and should be secured or removed before deployment -->
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="WcfServiceLibraryforADD.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>
</behaviors>
</system.serviceModel>
</configuration>
【问题讨论】:
-
你不能创建一个
ObjectClass实例吗? -
@AmiramKorach 我按照 akton 的建议尝试过,但仍然出现错误。请在 akton 答案中查找错误。
-
只是一个观察,但你真的很想被黑客入侵。您的 SQL 只接受用户输入的任何内容并将其串在一起:让您面临 SQL 注入攻击。使用 SQL 参数解决此问题。
标签: c# asp.net wcf wcf-binding wcf-client