I created this for users who are new to WCF and would like to get a quick demo up and running. I'm not going to go into too many details. The overall goal of this is for a developer to quickly get things up and running. Hopefully I can shed some light on some of the issues I ran into and save somebody else time for this specific scenario.
Let's get started.
Requirements:
- Must be hosted on IIS 7.5 running in integrated mode for .NET Framework Version 4.0.
- User needs access to services running over SSL/HTTPS.
- Authentication is not required since this is internal.
- The client will run on .NET, although other clients are valid, this demo will use a console application as a client.
There will be 3 sections:
- Setting up the service.
- Setting up IIS.
- Setting up the client.
Setting Up the Service
Add a WCF service called MyService.svc:
After adding the WCF service, Visual Studio will create 3 files.
- MyService.svc (Markup, Leave this Alone)
- MyService.svc.cs (Implementation of the WCF Contract)
- IMyService (WCF Contract)
Create a class called MyCustomData then paste the following into your new class. This class is a DataContract between the client and server. We are telling the WCF server that this should be accessible on client machines accessing our services. We will later use this class both on both the client and server of our WCF implementation.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.Serialization;
namespace WCF_Demo
{
[DataContract]
public class MyCustomData
{
///
/// Public Properties
///
[DataMember]
public int MyID { get; set; }
[DataMember]
public string MyStringValue1 { get; set; }
[DataMember]
public string MyStringValue2 { get; set; }
[DataMember]
public MyCustomData OriginalValue
{
get
{
return originalValue;
}
set
{
originalValue = value;
}
}
///
/// Private Properties
///
private MyCustomData originalValue;
public MyCustomData(MyCustomData data)
{
originalValue = data;
}
public MyCustomData()
{
;
}
}
}
Your solution should look like this now:
I've pasted my web.config for this solution below. You can copy and paste it over your web.config. The key points to understand are the '<system.serviceModel>' section of the config and the children of '<system.serviceModel>'. The important sections pertaining to WCF are highlighted below in lime green. The initial service declaration is the important part.
<?xml version="1.0"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<system.web>
<customErrors mode="Off"></customErrors>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<!-- Services -->
<services>
<service behaviorConfiguration="TestHttpsBehavior" name="WCF_Demo.MyService"> <endpoint address="" binding="wsHttpBinding" contract="WCF_Demo.IMyService" bindingConfiguration="TestHttpsBinding">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange"></endpoint>
<host>
<baseAddresses>
<add baseAddress="https://localhost/MyService" />
</baseAddresses>
</host>
</service>
</services>
<!-- Bindings -->
<bindings>
<wsHttpBinding>
<binding name="TestHttpsBinding">
<security mode="Transport">
<transport clientCredentialType="None"></transport>
</security>
</binding>
</wsHttpBinding>
</bindings>
<!-- Behaviors -->
<behaviors>
<serviceBehaviors>
<behavior name="TestHttpsBehavior">
<serviceMetadata httpGetEnabled="false" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
</configuration>
Setting Up IISMake sure that WCF is installed:
Control Panel -> Programs and Features -> Turn Windows Features on or off
If you did not have WCF installed after installing. Run "aspnet_regiis.exe -i" at the command prompt under "C:\Windows\Microsoft.NET\Framework\v2.0.50727".
Create Self Signed Server Certificate:
Under IIS Manager
IIS -> Server Certificates -> Create Self-Signed Certificate Request
Give your certificate a name.
Create a new site for the WCF-Demo you created earlier:
The root directory for your new site will project root directory originally created earlier.
Add new HTTPS binding for your new site:
Test Your Service:
Hit your service over HTTPS with your browser, you should get the following if everything wen correctly.
Setting Up the Client
Create a new Console application the add service reference
Your static void main should look like this to test the client.
static void Main(string[] args)
{
//reference the new service client
MyServiceClient client = null;
try
{
//this is needed to enable the client to trust the certificate
ServicePointManager.ServerCertificateValidationCallback =
new RemoteCertificateValidationCallback(delegate(object o, X509Certificate cert, X509Chain chain, SslPolicyErrors policyErrors){
return true;
});
//create an instance
client = new MyServiceClient();
//call method on service and get result
string result = client.DoWork();
Console.WriteLine(result);
Console.ReadLine();
}
finally
{
//make sure to always close the client after using it
if (client != null && !(client.State == System.ServiceModel.CommunicationState.Closed))
{
client.Close();
}
}
}
No comments:
Post a Comment