![]() |
|
|
#1 |
|
Join Date: Mar 2009
Posts: 23
|
Tutorial: How to host a WCF service on DASP
I've had trouble figuring out WCF services and even more trouble getting this hosted on DASP. I really feel that DASP should have taken the initiative and put such tutorials up themselves, but in the absence I have prepared one which shows how to build a bare bones WCF and then I sow you exactly how to publish it to your site hosted on discountasp.net. I also attached the the code. You will need to configure web.config to set the address of your website.
Most of the this I got service I got from these 4 (amazing) PluralSight video-casts: Creating WCF Service (Part 1) Creating WCF Client (Part 2) Configuring Services with EndPoints (Part 3) Hosting WCF Services in IIS (Part 4) If you follow those then the only changes you need to make at the end are to the web.config file. I'll post mine below although I use slightly different names to better help me distinguish between which class is which. ========================== Contents: ========================== Section 1: Over view of steps Section 2: My Configuration and assumptions Section 3: Detailed step-by-step process Section 4: The set of code snippets you should have Section 5: My Web.config ========================== Section 1: Over view of steps ========================== ////Part I (Setting Up the service in VS2008) 1-2: Create the service Library Project 3-4: Create the Data object which will be passed through your WCF service 5: Create a service CONTRACT used by the client to INTERFACE with the service implementation 7-8: Implement the methods specified in the service CONTRACT 9: Set up the serviceModel for the service 10: Specify what protocol will be used to interact with the service (ie: basicHttpBinding, wsHttpBinding...) 11: Specify the endpoint address for the service discovery (mexHttpBinding) 12-13: Test local access to the service using Visual Studio's "WcfTestClient.exe" util. /////Part II (Setting Up the service to be hosted on DASP) 14: Create a WCFService Website that will be published on discountasp.net 15: Set up your .svc service that you can point to in your browser. (**Don't forget to remove the "code behind" in the .svc**) 16: Copy the files created in your Servicelibrary to the WebSiteService project 17: Remove everything from web.config that is not needed to run our service. (**Dont forget to add in the base address to your dasp website**) 18: Set the binding to basicHttpBinding (Not sure if wsHttpBinding also works) 19: Copy the website folder and all of its contents to your discoutasp.net root directory 20: Make your application service directory an application in DASP the host panel. 21: Disable basic authentication using the IIS7.0 application tool (**I don't know if you can do it without IIS7??**) 22: Test remote access to the service using Visual Studio's "WcfTestClient.exe" util. ================================== Section 2: My Configuration and assumptions ================================== 1) VisualStudio=2008(SP1) -VSRoot="C:\Program Files\Microsoft Visual Studio 9.0" 2) .NET=3.5(SP1) 3) discountasp.net's Web server=IIS7.0 4) OS=WindowsXP 5) Project Root: C:\ 6) Project Name: EvalServiceLibrary 7) Using IIS7.0 Manager =============================== Section 3: Detailed step-by-step process =============================== 1) Open V2008. Select: File->New->Project -Select: Project types="Visual C#"->"WCF" -Select: Templates="WCF ServiceLibrary" -Set: Name="EvalServiceLibrary" -Set: Location="C:\" -CheckBox: Create directory for solution=true -Set: Solution Name="EvalServiceLibrary" -Select: "Okay" 2) In "SolutionExplorer": Delete files {IService1.cs, Service1.cs} //We will create our own 3) In "Solution Explorer" RightClick on Solution "EvalService Library" -Select: "Add New Item" -Select: Categories="Visual C# Items" -Select: Templated="Class" -Set: Name="EvalDataObject.cs" -Select: "Okay" 4) In "EvalDataObject.cs" code window delete all, replace with the following -Copy and Paste Code Section: EvalDataObject.cs (See Code below) 5) In "Solution Explorer" RightClick on Solution "EvalServiceLibrary" -Select: "Add New Item" -Select: Categories="Visual C# Items" -Select: Templated="Interface" -Set: Name="IEvalServiceInterfaceContract.cs" -Select: "Okay" 6) In "IEvalServiceInterfaceContract.cs" code window: -Delete all code. -Copy and Paste Code Section: IEvalServiceInterfaceContract.cs (See Code below) 7) In "Solution Explorer" RightClick on Solution "EvalService Library" -Select: "Add New Item" -Select: Categories="Visual C# Items" -Select: Templated="Class" -Set: Name="EvalServiceImplementationOfContract.cs" -Select: "Okay" 8) In "EvalServiceImplementationOfContract.cs" code window: -Delete all code. -Copy and Paste Code Section: "EvalServiceImplementationOfContract.cs" (See Code below) 9) In "Solution Explorer" under "EvalService Library" -RightClick on: App.config -Select: "Edit WCF Configuration" -In "Configuration Tree" -Select: "Services"->"EvalServiceLibrary.Service1" -In "Service: EvalServiceLibrary.Service1" "Tree -Click on "General"->"Name" field -Open Dialog Box in "Name" Field -Navigate to: bin\Debug -DoubleClickOn: EvalServiceLibrary.dll -Select: EvalServiceLibrary.EvalServiceImplementationOfCont ract -Select: "OK" 10) Still in the in the "WCF Cofiguration" Dialoge: -Select: "EvalServiceLibrary.EvalServiceImplementationOfCon tract"->"EndPoints" -Select first "(Empty Name)" in tree menu -In "Service Endpoint" menu: -Select: Endpoint Properties -Click on address text field -Set Address="ws" -Double Click on "Contract" Field -Navigate to: bin\Debug -DoubleClickOn: EvalServiceLibrary.dll -Select: EvalServiceLibrary.EvalServiceImplementationOfCont ract 11) Still in the in the "WCF Cofiguration" -Select the second "(Empty Name)" in tree menu -In the "Service Endpoint" menu: -Select: Endpoint Properties -Click on address text field -Set Address="mex" 12) Close "WCF Configuration" and accept save changes -Rebuild -Select "F5" to run the "WCF Test Client" 13) When "WCF Test Client" Dialogue pops up -DoubleClick->"SubmitEval()" in Tee menue -Set: Comments="My Comments" -Set: Submitted="Me" -Click: "Invoke" Button. -Select: "GetEvals()" -Click: "Invoke" Button. -Observe the data you submitted and retrieved from your service. -Stop running (Debig-> 14) In main workbench select "File"->"Add New Web Site" -Select Template="WCF Service" -Set Filename="C:\EvalServiceWebSite" -Set Language="Visual C#" -Click "Okay" 15) Go to "Solution Explorer" under "C:\EvalServiceWebSite"->App_Code -Delete {IService.cs, Service.cs} -Select: Service.svc -Rename: Service.svc -> EvalNetService -RightClick on "EvalNetService.svc"->"View In Markup" -Delete all code. -Copy and Paste Code Section: EvalService.svc (See Code below) -Rebuild 16) Go to "Solution Explorer" under -RightClick on "C:\EvalServiceWebSite" -Select: "Add Reference"->(Tab)"Projects"->"EvalServiceLibrary" //This copies the .dll and EvalServiceLibrary.pbd to your website 16) In "Solution Explorer" under "EvalServiceLibrary"->"C:\EvalServiceWebSite" -RightClick on: Web.onfig -Select: "Edit WCF Configuration" -In "Configuration Tree" -Select: "Services"->"Service" -In "Service: EvalServiceLibrary.Service1" "Tree -Click on "General"->"Name" field -Open Dialog Box in "Name" Field -Navigate to: "C:\EvalServiceLibrary\bin\Debug" in Library Directory -DoubleClickOn: EvalServiceLibrary.dll -Select: EvalServiceLibrary.EvalServiceImplementationOfCont ract -Select: "OK" 17) Go to the web.config text editor. -Delete everything not within the <configuration><system.ServiceModel> tags -Delete the "<host>" tags and their contents (If there is one) -Delete the "<identity><dns value="localhost"/></identity> -Within the <configuration><system.ServiceModel> -Copy and Paste Code Section: "serviceHostingEnvironment" (See Code below) 18) Go back to web.config "WCF Configuration" editor -Select: Services->EvalServiceLibrary.EvalServiceImplementationOFCon tract->Endpoints -For wsHttpBinding -Set: Address="basic" -Set: binding="basicHttpBinding" -Click on "Name" field & Open Dialog Box -Navigate to: "c:\evalservicewebsite\Bin" in website service Directory -DoubleClickOn: EvalServiceLibrary.dll -Select: "EvalServiceLibrary.IEvalServiceInterfaceContr act" -Select: "OK" 19) Open up FTP and login to your discount.asp site -Drag and drop "c:\EvalServiceWebSite" to your base FTP directory. 20) Login to discount.asp.net's "Hosting Control Panel" -Select: Tools&Utilities->Web Application Tool -In tree menue select: "EvalServiceWebSite" -Click the Add Application button. -Go to Tools&Utilities->IIS Tools -Restart WebServer. 21) Start the IIS 7.0 Manager on your computer -Connect to your webserver -Click on your root directory -Select "Authentication" -RightRightClick on "Basic" and "disable" authentication 22) Point browser to: "http://yourdomainname.com/EvalServiceWebSite/EvalService.svc" -Ensure you have a page which asks you to create a client with svcutil.exe -Go to you Visual Studio's instalation directoy -Search for "WcfTestClient.exe" -Open command line and run "wcfTestClient.exe http://yourdomain.com/EvalServiceWebSite/EvalService.svc" |
|
|
|
|
|
#2 |
|
Join Date: Mar 2009
Posts: 23
|
(I also attached this as a text file since the tab formatting has been lost here)
======================================= Section 4: The set of code snippets you should have ======================================= ////START: EvalDataObject.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.Serialization; namespace EvalServiceLibrary { [DataContract] class EvalDataObject { [DataMember] public string Id; [DataMember] public string Submitter; [DataMember] public string Comments; [DataMember] public DateTime TimeSubmitted; } } ////END: EvalDataObject.cs //START: IEvalServiceInterfaceContract.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; namespace EvalServiceLibrary { [ServiceContract] interface IEvalServiceInterfaceContract { [OperationContract] void SubmitEval(EvalDataObject eval); [OperationContract] List<EvalDataObject> GetEvals(); [OperationContract] void RemoveEval(string id); } } //END: IEvalServiceInterfaceContract.cs ////START: EvalServiceImplementationOfContract.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; namespace EvalServiceLibrary { //Only a single list of service created at runtime [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)] class EvalServiceImplementationOfContract : IEvalServiceInterfaceContract { List<EvalDataObject> evals = new List<EvalDataObject>(); #region IEvalServiceInterfaceContract Members public void SubmitEval(EvalDataObject eval) { eval.Id = Guid.NewGuid().ToString(); evals.Add(eval); } public List<EvalDataObject> GetEvals() { return evals; } public void RemoveEval(string id) { evals.Remove(evals.Find(e => e.Id.Equals(id))); } #endregion } } ////END: EvalServiceImplementationOfContract.cs ////START: EvalService.svc <%@ ServiceHost Language="C#" Debug="true" Service="EvalServiceLibrary.EvalServiceImplementat ionOfContract"%> ////END: EvalService.svc ////START: serviceHostingEnvironment <serviceHostingEnvironment> <baseAddressPrefixFilters> <add prefix="http://wagnerlab.com" /> </baseAddressPrefixFilters> </serviceHostingEnvironment> ////END: serviceHostingEnvironment ===================================== Section 5: My Web.config ===================================== <?xml version="1.0"?> <configuration> <system.serviceModel> <services> <service behaviorConfiguration="ServiceBehavior" name="EvalServiceLibrary.EvalServiceImplementation OfContract"> <endpoint address="basic" binding="basicHttpBinding" bindingConfiguration="" contract="EvalServiceLibrary.IEvalServiceInterface Contract" /> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> </service> </services> <serviceHostingEnvironment> <baseAddressPrefixFilters> <add prefix="http://yourdomain.com" /> </baseAddressPrefixFilters> </serviceHostingEnvironment> <behaviors> <serviceBehaviors> <behavior name="ServiceBehavior"> <serviceMetadata httpGetEnabled="true"/> <serviceDebug includeExceptionDetailInFaults="false"/> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> </configuration> |
|
|
|
|
|
#3 |
|
Join Date: Mar 2009
Posts: 23
|
**** Warning: I am seeing some spaces inserted where they are not supposed to be. For example in:
"Service="EvalServiceLibrary.EvalServiceImplementa t ionOfContract" It's probably best to use the text file for any cut-and-past operations. |
|
|
|
|
|
#4 |
|
Join Date: Apr 2009
Location: Minnesota
Posts: 2
|
This looks to be extremely useful and exactly what I needed. Thank you!
I must compliment you on your thorough attention to detail. I downloaded the attached EvalServiceLibrary.zip but it seems to be corrupted. Has anyone else tried it? Regards, Art Update: I just discovered that while I cannot open the Zip file using WinZip, I can open and extract the files using 7-Zip. Last edited by arthg; 05-01-2009 at 01:43 PM. Reason: new information |
|
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Tutorial: Create a ADO.net Data Service | rdlecler | ASP.NET | 7 | 05-07-2009 11:22 AM |
| HELP!!!!!!!!!! MEMBERSHIP/ROLES(CAN'T USE LOGIN ACCOUNT ON HOST SERVER BUT WORKS ON LOCAL HOST) | Dipset | Databases | 1 | 05-08-2008 11:13 AM |
| PLS HELP!!!!!!!!!! MEMBERSHIP/ROLES(CAN'T USE LOGIN ACCOUNT ON HOST SERVER BUT WORKS ON LOCAL HOST) | Dipset | Databases | 2 | 05-08-2008 09:46 AM |
| Setting up FeedBurner's MyBrand service with DASP | kbeyer | Hosting Services / Control Panel | 2 | 01-16-2007 07:40 AM |
| Web Service host is requiring both a DER X.509 digital certificate and a PKCS12 private key | knowledgesc | ASP.NET WebServices | 0 | 02-08-2005 08:45 AM |