代码之家  ›  专栏  ›  技术社区  ›  Pavan V Parekh

如何将参数作为调用WCF DataContract的对象传递

  •  0
  • Pavan V Parekh  · 技术社区  · 12 年前

    -这个WCFSeviceLibrary的My接口类

    在此接口类中,中的IService1将OperationContract声明为一个ObjectClass类型方法。

    并为集合类型创建此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; }
                }
    
            }
    

    服务.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;
            }
    }
    

    此Web应用程序页面的My Btn_Click类

     protected void Add_Click(object sender, EventArgs e)
            {
                ServiceReference1.Service1Client srv = new ServiceReference1.Service1Client();
    
    
                srv.SetDataUsingDataContract();
    
    }
    

    我不知道如何设置srv.SetDataUsingDataContract()方法的参数。

    应用程序配置文件

    <?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>
    
    1 回复  |  直到 12 年前
        1
  •  2
  •   akton    12 年前

    创建 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);
    }
    

    客户端生成代码应该创建 对象类 类,以便调用方引用。