【问题标题】:How to Pass Argument as Object for call WCF DataContract如何将参数作为对象传递以调用 WCF DataContract
【发布时间】: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


【解决方案1】:

创建一个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 类供调用者引用。

【讨论】:

  • 我尝试上面的代码,但得到以下错误。 The best overloaded method match for 'WebApplicationvc.ServiceReference1.Service1Client.SetDataUsingDataContract(WebApplicationvc.ServiceReference1.ObjectClass)' has some invalid argumentsArgument '1': cannot convert from 'WebApplicationvc.ObjectClass' to 'WebApplicationvc.ServiceReference1.ObjectClass'
  • 您可能在客户端中定义了两次ObjectClass。一个是您自己编写的,另一个是通过添加服务引用生成的。
  • @PavanVParekh 在生成的客户端 (WebApplicationvc.ServiceReference1.ObjectClass) 中引用 ObjectClass 的版本。如果您重复了该定义,请将其从 WebApplication 中删除。您可以拥有两个具有相同名称的类,但在内部,即使它们共享相同的成员,它们仍然是不同的。
  • @akton 和 @AmiramKorach 谢谢你,是的,从 Web 应用程序中删除了重复的对象类,但现在我从 Service1Client srv = new Service1Client(); 得到以下错误运行时 运行时错误端点配置无法加载合同“ServiceReference1.IService1”的部分,因为找到了该合同的多个端点配置。请按名称指明首选端点配置部分。
  • @Amiram Korach 我检查了配置文件。 我的 Appconfig 文件 两个端点(1 是默认端点,2 是用于 mex) 请看我添加 Appconfig 代码的问题。
猜你喜欢
  • 1970-01-01
  • 2013-02-20
  • 2020-09-23
  • 2015-04-10
  • 2019-10-19
  • 2013-09-10
  • 1970-01-01
  • 1970-01-01
  • 2023-03-05
相关资源
最近更新 更多