Step by step in WCF (1)

Source: Internet
Author: User

Step 1:

We will recommend that you use the WCF Service and a client. Finally, publish it to IIS. A nbuilder. dll is required before you start creating the WCF Service. With this, we can avoid the trouble of creating databases.

1. Create a new solution, right-click to add a new website, select the WCF Service, and click the address.

2. Introduce nbuilder to the project and create two data table classes tblproduct. CS and tblproductinventory. CS under app_code.

Usingsystem;
Using system. Collections. Generic;
Using system. LINQ;
Using system. Web;

Namespaceproducts
{
/// <Summary>
/// Summary of tblproduct
/// </Summary>
Public class tblproduct
{
Public tblproduct ()
{
//
// Todo: add the constructor logic here
//
}

Publicint productid;

Publicstring name;

Publicstring productnumber;

/// <Summary>
/// 0 = the product is purchased. 1 = the product is produced internally.
/// </Summary>
Public bool makeflag;

/// <Summary>
// 0 = the product cannot be sold. 1 = the product can be sold.
/// </Summary>
Public bool finishedgoodsflag;

Publicstring color;

//// Minimum inventory.
Public int safetystocklevel;

/// <Summary>
/// Trigger the inventory level of the purchase order or work order.
/// </Summary>
Public int reorderpoint;

/// <Summary>
/// Standard cost of the product.
/// </Summary>
Public decimal standardcost;

/// <Summary>
/// Sales price.
/// </Summary>
Public decimal listprice;

/// <Summary>
/// Product specifications.
/// </Summary>
Public String size;

/// <Summary>
/// Measurement unit of the size column.
/// </Summary>
Public String sizeunitmeasurecode;

/// <Summary>
/// Measurement unit of the weight column.
/// </Summary>
Public String weightunitmeasurecode;

/// <Summary>
/// Product weight.
/// </Summary>
Public decimal weight;

/// <Summary>
/// Number of days required for product production.
/// </Summary>
Public int daystomanufacture;

/// <Summary>
/// R = Flat M = mountain T = Travel s = Standard
/// </Summary>
Public String productline;

/// <Summary>
/// H = high M = medium L = low
/// </Summary>
Public string class;

/// <Summary>
// W = Women's M = Men's u = General
/// </Summary>
Public String style;

/// <Summary>
/// Subcategory of the product. The foreign key that points to productsubcategory. productsubcategoryid.
/// </Summary>
Public int productsubcategoryid;

/// <Summary>
/// The product model to which the product belongs. The foreign key that points to productmodel. productmodelid.
/// </Summary>
Public int productmodelid;

/// <Summary>
/// Date on which the product starts to be sold.
/// </Summary>
Public datetime sellstartdate;

/// <Summary>
/// Date on which the product is discontinued.
/// </Summary>
Public datetime sellenddate;

/// <Summary>
/// Date of product suspension.
/// </Summary>
Public datetime discontinueddate;

/// <Summary>
/// The last update date and time of the row.
/// </Summary>
Public datetime modifieddate;
}
}

Usingsystem;
Using system. Collections. Generic;
Using system. LINQ;
Using system. Web;

Namespaceproducts
{
/// <Summary>
/// Summary of productinventory
/// </Summary>
Public class tblproductinventory
{
Public tblproductinventory ()
{
//
// Todo: add the constructor logic here
//
}

/// <Summary>
/// Product ID. The foreign key that points to product. productid.
/// </Summary>
Public int productid;

/// <Summary>
/// Inventory position ID. The foreign key pointing to location. locationid.
/// </Summary>
Public int locationid;

/// <Summary>
/// Storage space in the inventory location.
/// </Summary>
Public String shelf;

/// <Summary>
/// Storage container on the shelf in the inventory position.
/// </Summary>
Public int bin;

/// <Summary>
/// Number of products in the inventory position.
/// </Summary>
Public int quantity;

/// <Summary>
/// The last update date and time of the row.
/// </Summary>
Public datetime modifieddate;
}
}

2. Rename iservice. CS to iproductsservice. CS.

Usingsystem;
Using system. Collections. Generic;
Using system. LINQ;
Using system. runtime. serialization;
Using system. servicemodel;
Using system. servicemodel. Web;
Using system. text;

Namespaceproducts
{
// Note: You can use the "RENAME" command on the "refactoring" menu to change the Interface Name "iservice" in the Code and configuration file at the same time ".
[Servicecontract]
Public interface iproductsservice
{

// Get the product number of every product
[Operationcontract]
List <string> listproducts ();
// Get the details of a single product
[Operationcontract]
Productdata getproduct (string productnumber );
// Get the current stock level for a product
[Operationcontract]
Int currentstocklevel (string productnumber );
// Change the stock level for a product
[Operationcontract]
Bool changestocklevel (string productnumber, short newstocklevel, string shelf, int bin );
}
}

3. Rename service. CS productsservice. CS

Usingsystem;
Using system. Collections. Generic;
Using system. LINQ;
Using system. runtime. serialization;
Using system. servicemodel;
Using system. servicemodel. Web;
Using system. text;
Using fizzware. nbuilder;
// Note: You can use the "RENAME" command on the "refactoring" menu to change the class name "service" in the code, service, and configuration file at the same time ".
Namespace Products
{
[Datacontract]
Public class productdata
{
[Datamember]
Public string name;
[Datamember]
Public String productnumber;
[Datamember]
Public String color;
[Datamember]
Public decimal listprice;
}

Publicclass productsserviceimpl: iproductsservice
{
Static ilist <tblproduct> PS = builder <tblproduct>. createlistofsize (50). Build ();
Static ilist <tblproductinventory> Pis = builder <tblproductinventory>. createlistofsize (50). Build ();

Publiclist <string> listproducts ()
{
VaR P = ps. Select (item => item. productnumber). tolist ();

Returnp;
}

Publicproductdata getproduct (string productnumber)
{

Productdata = NULL;
Tblproduct matchingproduct = ps. First (item => string. Compare (item. productnumber, productnumber) = 0 );

Productdata = new productdata ()
{
Name = matchingproduct. Name,
Productnumber = matchingproduct. productnumber,
Color = matchingproduct. color,
Listprice = matchingproduct. listprice
};

Returnproductdata;
}

Public int currentstocklevel (stringproductnumber)
{
Int stocklevel = 0;

// Calculate the sum of all quantities for the specified product
Stocklevel = (from Pi in Pis
Join P in PS
On pi. productid equals P. productid
Where string. Compare (P. productnumber, productnumber) = 0
Select (INT) PI. Quantity). sum ();

Returnstocklevel;
}

Publicbool changestocklevel (string productnumber, short newstocklevel, string shelf, int bin)
{
// Var Pr = builder <tblproduct>. createlistofsize (50). Build ();
// Var pri = builder <tblproductinventory>. createlistofsize (50). Build ();
Int productid = (from P in PS
Where string. Compare (P. productnumber, productnumber) = 0
Select P. productid). First ();
// Find the productinventory object that matches the parameters passed
// In to the operation
Tblproductinventory productinventory = PIs. First (
Pi => string. Compare (PI. shelf, shelf) = 0 &&
Pi. bin = bin & Pi. productid = productid );
// Update the stock level for the productinventory object
Productinventory. Quantity + = newstocklevel;
// Save the change back to the database

Returntrue;
}
}
}

4. Rename service. SVC to productsservice. SVC.

<% @ Servicehost Language = "C #" DEBUG = "true" service = "products. productsserviceimpl" codebehind = "~ /App_code/productsservice. cs "%>

5. Create a console client named productsclient, and name the above WCF Service named productsservice. Add the code in static void main (string [] ARGs:

Staticvoid main (string [] ARGs)
{
Using (productsserviceclient proxy = new productsserviceclient ())
{
// Test the operations in the service
// Obtain a list of all products
Console. writeline ("Test 1: List all products ");
String [] productnumbers = proxy. listproducts ();
Foreach (string productnumber in productnumbers)
{
Console. writeline ("number: {0}", productnumber );
}
Console. writeline ();

Console. writeline ("Test2: display the details of a product ");
Productdata Product = proxy. getproduct ("productnumber1 ");
Console. writeline ("number: {0}", product. productnumber );
Console. writeline ("Name: {0}", product. Name );
Console. writeline ("color: {0}", product. Color );
Console. writeline ("Price: {0}", product. listprice );
Console. writeline ();

Console. writeline ("test3: display the stock level of a product ");
Int numinstock = proxy. currentstocklevel ("productnumber2 ");
Console. writeline ("current stock level: {0}", numinstock );
Console. writeline ();

Console. writeline ("test4: Modify the stock level of a product ");
If (proxy. changestocklevel ("productnumber3", 100, "shelf3", 3 ))
{
Numinstock = proxy. currentstocklevel ("productnumber3 ");
Console. writeline ("stock changed. Current stock level: {0}", numinstock );
}
}
Console. readkey ();
}

Set productsclient as the startup project to see the running result.

 

Right-click the WCF project, select publish website, and click "..." in the target location bar to open the dialog box. Select "Local IIS", click "defaultweb site", click "Create web application" in the upper right corner, and name it productsservice.

OK. You have published the application successfully.

Then, in the productsclient project, open app. config. The endpoint address in <client> </client> points to the IIS service again. For example

<Client>
<Endpoint address = "http: // localhost/productsservice. SVC"
Binding = "basichttpbinding" bindingconfiguration = "basichttpbinding_iproductsservice"
Contract = "productsservice. iproductsservice" name = "basichttpbinding_iproductsservice"/>
</Client>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.