Showing posts with label WCF. Show all posts
Showing posts with label WCF. Show all posts

Saturday, March 3, 2012

how to upload a file in wcf

Here i will show how to upload a file in WCF serve rice.The streaming concept is used to implement a wcf service to upload a file.I have created a wcf project i:e UploadFile.svc then Open the IUploadFile.cs and add following code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.IO;
[ServiceContract]
public interface IUploadFile
{
    [OperationContract]
    string FileUpload(Stream inputStream);
    [OperationContract]
    Stream FileDownload(string fId);
    [OperationContract]
    string[] GetFiles();
}
CodeBehind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.IO;
using System.Web;

public class UploadFile : IUploadFile
{
    private const string PATH = @"C:\wcf\download";

    private string GetDirectoryinfoPath()
    {
        return PATH;
    }
    public string FileUpload(System.IO.Stream inputStream)
    {
        string fID = string.Format(@"{0}\{1}.txt", GetDirectoryinfoPath(), Guid.NewGuid().ToString());
        StreamReader sreader = new StreamReader(inputStream);
        string filecontents = sreader.ReadToEnd();
        File.WriteAllText(fID, filecontents);
        return fID;
    }
   public string[] GetFiles()
    {
        return new DirectoryInfo(GetDirectoryinfoPath()).GetFiles().Select(x => x.FullName).ToArray();
    }
 
}

Configuration File:
Now we have to give some configuration settings for wcf service.
<system.servicemodel>
<services>
<service behaviorconfiguration="streamServiceBehaviour" name="UploadFile">
<endpoint address="" binding="basicHttpBinding" bindingconfiguration="streamBindingConfig" contract="IUploadFile">
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange">
</service>
</services>
<behaviors>
<servicebehaviors>
<behavior name="streamServiceBehaviour">
<servicedebug includeexceptiondetailinfaults="true">
<servicemetadata httpgetenabled="true">
</behavior>
</servicebehaviors>
</behaviors>
<bindings>
<basichttpbinding>
<binding name="streamBindingConfig" transfermode="Streamed">
</binding>
</basichttpbinding>
</bindings>
</system.servicemodel>


Then i have add a new with file upload control.Then add the following code to code behind.
using System;

using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class UploadExample : System.Web.UI.Page
{
    StreamService.FileStreamClient wclient = new StreamService.FileStreamClient();
    string fileId = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnUpload_Click(object sender, EventArgs e)
    {
      fId=wclient.FileUpload(new MemoryStream(FileUpload1.FileBytes));
    }
  
}

Sunday, February 19, 2012

Basics of wcf


WCF stands for Windows communication foundation.SOAP is an attribute it is used on web method.It is recommended to work with WCF 3.0,3.5,3.5sp1,and 4.0 .Data with WCF is also recommended using LINQ to SQL and ADO.net entity framework.WCF is completely configuration based and many tasks in WCCF can be done with configuration.WCF even supports consuming classes pragmatically writing code to perform it's actions but nobody uses WCF with code because configuration based is a big advantage.

WCF supports attribute based programming for every behaviour of server and it has lot of attributes along with properties to fulfill the distributed application environment.
[attribute] attributes are classes
{
}
WCF unified model is prepared with simple ABC model
A-Address
B-Binding
C-Contract
Now i will show you how we are communicating in WCF.The Binding concept is used to communicate in WCF.TCP,SOAP,Meta data binding these are computer terms.WCF generate WSDL based on Contract.Contact will come interface what will gives that's it
A+B+C=End point in WCF
you can have multiple ENDPOINTS .To serve the same Contracts different types of clients.
Address:
A network address where the End point resides.
Ex:
http://test.com/services/test.svc
net.tcp://192.168.1.10:6000/service
net.msmq://myservice/test

Binding:Specifies how the End point communicate with the world
-Transport(ex:HTTP,TCP)
-Encoding(Text,Binary,MTOM)
-Secirity options(SSL,message security)

EX:
BasicHttpBinding ,NetTcpBinding ..etc
Contract:Specifies what the endpoint communicator
define the things like
-message exchange patterns
-service operations
-Behaviour(exchange meta data-Data,Authorisation-etc)

Wednesday, February 15, 2012

how to Implement wcf service

Vs 2010 provides different templates for develop WCF services .One it provides in file--> new website-->WCF service using which we can design develop WCF service in simplified manner by default this template will create One .SVC file fallowed by two .cs files.Most importantly End points for this new service are also created in web.config file.We can add more services using add new item WCF service template for every service that we create automatically.End points along with other configuration settings are written system.Service model is the tag where all these End points are defined.we can directly add more end points otherwise use wcf configuration editor utility to modify or add more points.

Here i will shown how to create a simple web service .For this we have to fallow the below steps.

start wcf service and add two more services called Products and category.These two are services we will use to provide categories and Products information.

The back end layer is performed using LINQ SQL and entity framework objects.so add entity framework classes using add new item from App code and choose Ado.net entity data model.EDM is very complex to create for our application but with EDM designer provided with VS to maximum extend we can simplify the creation of entities.

One EDM class are lINQ to SQL classes are prepared.Then we can provide operation contracts fallowed by implementation in our service and it's class go to products and add the following operation contract

[operation contract]
void Dowork()
[operation contract]
list GetProducts();
[operation contract]
products GetProducts(int product id);
[operation contract]
list GetProductsIn category(int cat id);

Now go to implementation class of this contract i:e product class.Implementation of each method like this

public listGetProducts()
{//linq to entities
Eshopmodel.Eshopentities obj=new Eshopmodel.EshopEntities();

var x=from n in obj.Products select n;
return x.Tolist();
}
public Eshopmodel.Products GetProducts(int productid)
{
Eshopmodel.EshopEntities obj=new Eshopmodel.EshopEntities();
var x=(from n in obj.products where n.productid==productid select n).First or default();
return x;
}
public Eshopmodel.Products GetProductsInCategory(int catid)
{
Eshopmodel.EshopEntities obj=new Eshopmodel.EshopEntities();
var x=(from n in obj.products where n.productid==catid select n).First or default();
return x.Tilist();
}

Bel