C# 如何将参数作为调用WCF DataContract的对象传递
l -这是我的WCFSeviceLibrary接口类 在此接口中,将OperationContract中的IService1类声明为一个ObjectClass类型方法 并为集合类型创建此ObjectClass类。作为甘尼瑞克C# 如何将参数作为调用WCF DataContract的对象传递,c#,asp.net,wcf,wcf-binding,wcf-client,C#,Asp.net,Wcf,Wcf Binding,Wcf Client,l -这是我的WCFSeviceLibrary接口类 在此接口中,将OperationContract中的IService1类声明为一个ObjectClass类型方法 并为集合类型创建此ObjectClass类。作为甘尼瑞克 public interface IService1 { [OperationContract] ObjectClass SetDataUsingDataContrac
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文件
在这个类中,Implimate
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_点击类Web应用程序页面
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
实例,并将其作为参数传递给SetDataUsingDataContract
。例如:
protected void Add_Click(object sender, EventArgs e)
{
ServiceReference1.Service1Client srv = new ServiceReference1.Service1Client();
ObjectClass objectClass = new ObjectClass();
// Set properties
srv.SetDataUsingDataContract(objectClass);
}
客户端生成代码应该已经创建了一个
ObjectClass
类供调用方引用。您不能创建一个ObjectClass
实例吗?@AmiramKorach我已经按照阿克顿的建议尝试过了,但仍然出错。请找出阿克顿回答中的错误。这只是一个观察,但你真的很想被黑客攻击。您的SQL只接受用户键入的任何内容并将其串在一起:使您面临SQL注入攻击。通过使用SQL参数来解决这个问题<代码>与“WebApplicationvc.ServiceReference1.Service1Client.SetDataUsingDataContract(WebApplicationvc.ServiceReference1.ObjectClass)”匹配的最佳重载方法'具有一些无效参数和参数'1':无法从'WebApplicationvc.ObjectClass'转换为'WebApplicationvc.servicerence1.ObjectClass'
您可能在客户端中定义了两次ObjectClass
。一个是您自己编写的,另一个是通过添加服务引用生成的。@PavanVParekh引用生成的客户端中的ObjectClass版本(WebApplicationvc.ServiceReference1.ObjectClass)。如果已重复该定义,请将其从WebApplication中删除。您可以有两个名称相同的类,但在内部,即使它们共享相同的成员,它们仍然是不同的。@akton和@AmiramKorach谢谢您从web应用程序中删除重复的对象类是的,但现在我从Service1Client srv=new Service1Client()中得到以下错误运行时:代码>运行时错误是无法加载协定“ServiceReference1.IService1”的终结点配置节,因为找到了该协定的多个终结点配置。请按名称指明首选端点配置节。@Amiram Korach我有检查配置文件。我的Appconfig文件两个端点(1是默认值,2是mex)请看我的问题,我已经添加了Appconfig代码。