. net WCF simple example details (5)

Time:2020-1-16

This article shares a simple example of. Net WCF for your reference. The specific content is as follows

1. Create WCF project

2. The system automatically generates IWCFService

//Note: using the rename command on the refactoring menu, you can change the interface name iservice1 in both the code and the configuration file.
  [ServiceContract]
  public interface IWcfService
  {

    [OperationContract]
    string GetData(int value);

    [OperationContract]
    CompositeType GetDataUsingDataContract(CompositeType composite);

    //Todo: add your service operation here
  }


  //Use the data convention described in the following example to add a composite type to a service operation.
  [DataContract]
  public class CompositeType
  {
    bool boolValue = true;
    string stringValue = "Hello ";

    [DataMember]
    public bool BoolValue
    {
      get { return boolValue; }
      set { boolValue = value; }
    }

    [DataMember]
    public string StringValue
    {
      get { return stringValue; }
      set { stringValue = value; }
    }
  }

(1) Service contract: servicecontract and operationcontract

(2) Data contract: datacontract (class) and DataMember (property) are used on class and structure

(3) Message contract: messagecontract for SOAP messages

3. WCF service class


public class WcfService : IWcfService
  {
    public string GetData(int value)
    {
      return string.Format("You entered: {0}", value);
    }

    public CompositeType GetDataUsingDataContract(CompositeType composite)
    {
      if (composite == null)
      {
        throw new ArgumentNullException("composite");
      }
      if (composite.BoolValue)
      {
        composite.StringValue += "Suffix";
      }
      return composite;
    }
  }

4. Service profile

<system.serviceModel>
  <! -- configure binding node start -- >
  <bindings>
   <basicHttpBinding>
    <binding name="basicHttpBinding0" maxReceivedMessageSize="2147483647">
     <readerQuotas maxStringContentLength="2147483647"/>
     <security mode="None" />
    </binding>
   </basicHttpBinding>
   <netTcpBinding>
    <binding name="netTcpBinding0" maxReceivedMessageSize="2147483647">
     <readerQuotas maxStringContentLength="2147483647"/>
     <security mode="None" />
    </binding>
   </netTcpBinding>
   <wsHttpBinding></wsHttpBinding>
  </bindings>
  <! -- configure binding node end -- >
  
  <! -- configure service node start -- >
  <services>
   <! -- configure a service, where you can specify the service name -- >
   <service name="WcfServiceTest.WcfService">
    <endpoint address="aaa" binding="basicHttpBinding" bindingConfiguration="basicHttpBinding0"
     name="BasicHttpBinding_WcfService" contract="WcfServiceTest.IWcfService">
     <identity>
      <dns value="localhost"/>
     </identity>
    </endpoint>
    <endpoint address="" binding="netTcpBinding" bindingConfiguration="netTcpBinding0"
     name="NetTcpBinding_WcfService" contract="WcfServiceTest.IWcfService">
     <identity>
      <dns value="localhost"/>
     </identity>
    </endpoint>
   </service>
  </services>
  <! -- configure service node end -- >

  <behaviors>
   <serviceBehaviors>
    <behavior>
     <! -- to avoid leaking metadata information, set the following values to false -- >
     <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
     <! -- to receive fault exception details for debugging, set the following value to true. Set to false before deployment to avoid leaking exception information -- >
     <serviceDebug includeExceptionDetailInFaults="false"/>
    </behavior>
   </serviceBehaviors>
  </behaviors>
  <protocolMapping>
    <add binding="basicHttpsBinding" scheme="https" />
  </protocolMapping>  
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
 </system.serviceModel>

5. IIS deploy WCF Services

6. Add client project and service reference

7. Add WCF service in main program and call method

class Program
  {
    static void Main(string[] args)
    {
      var client = new WcfService.WcfServiceClient();
      try
      {
        var str = client.GetData(2046);
        Console. Writeline (string. Format ("content: {0}", STR));
        client.Close();
      }
      catch (Exception ex)
      {
        Console. Writeline ("exception! "";
        client.Abort();
      }
      Console.ReadLine();
    }
  }

8. Client profile


<system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_WcfService" />
      </basicHttpBinding>
      <netTcpBinding>
        <binding name="NetTcpBinding_WcfService">
          <security mode="None" />
        </binding>
      </netTcpBinding>
    </bindings>
    <client>
      <!--<endpoint address="http://localhost/WcfServiceTest/WcfService.svc"
        binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_WcfService"
        contract="WcfService.IWcfService" name="BasicHttpBinding_WcfService" />-->
      <endpoint address="net.tcp://localhost/WcfServiceTest/WcfService.svc"
        binding="netTcpBinding" bindingConfiguration="NetTcpBinding_WcfService"
        contract="WcfService.IWcfService" name="NetTcpBinding_WcfService">
        <identity>
          <dns value="localhost" />
        </identity>
      </endpoint>
    </client>
  </system.serviceModel>

The above is the whole content of this article. I hope it will help you in your study, and I hope you can support developepaer more.

Recommended Today

Yii turns off debug and saves the data. An error is reported

Today, after uploading the code to the server for testing, it was found that everything was normal, and then when debugging was turned off for testing again, an error was reported as follows: yii\base\UnknownPropertyException: Getting unknown property: front\modules\invoice\models\OrderRecord::invoice_id Finally, it is found that this is because this field is newly added by me. The Yii […]