I revisited this today as I am hunting for work again and I know I'm going to get asked about WCF. ASP.NET interviews seem to go here. OK, so I made a new WCF Service Application like so:
Let's look at the default .svc file inside:
using System;
namespace WcfService
{
public class Service1 : IService1
{
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;
}
}
}
There is another default file. It comes with both an interface for the service and a class for a serializable DTO type jammed into it. Note the attributes below as they are all important:
using System.Runtime.Serialization;
using System.ServiceModel;
namespace WcfService
{
[ServiceContract]
public interface IService1
{
[OperationContract]
string GetData(int value);
[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);
}
[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; }
}
}
}
I published this default project as is to a folder and then I took the files and prepped them as a web site in IIS Express. I then set up a separate MVC application and was able to latch onto the web service by adding a reference as suggested in the link I give at the top of this blog posting. In a Controller, I shoved in this code and set a breakpoint after it. I this confirmed that the types get populated as I expected. My adding of a reference allows the MVC app to have the DTO for the serializable type.
Service1Client foo = new Service1Client();
string bar = foo.GetData(42);
CompositeType baz = new CompositeType();
baz.BoolValue = true;
baz.StringValue = "baz";
CompositeType qux = foo.GetDataUsingDataContract(baz);
I am doing all of this with VS2012 and C# 4.5 and I noticed that beyond the two methods I utilize above that Async versions of both methods are exposed!
Pain points:
- "It is possible that a handler mapping is missing. By default, the static file handler processes all content." shows up when you visit the URL for your WCF service and you do not have WCF features turned on for IIS Express. Turn them on in Windows 8 at: charm bar > Search > Settings > Turn windows features on and off > .NET Framework 4.5 Advanced Services > WCF Services
- When I made a reference I ended up making the folllowing files in a folder:
- configuration.svcinfo
- configuration91.svcinfo
- MvcApplication.ServiceReference1.CompositeType
- Reference.cs
- Reference.svcmap
- Service1.disco
- Service1.wsdl
- Service1.xsd
- Service11.xsd
- Service12.xsd
- right-click on the reference and pick "Configure Service Reference..."
- uncheck the checkbox for "Reuse types in referenced assemblies" in the dialog box which appears
- get out of the dialog box and back to the Solution Explorer
- right-click on the reference and pick "Update Service Reference"
- This may reveal the trick to beating the environment-swapping problem I suggest here.
BasicHttpBinding binding = new BasicHttpBinding();
EndpointAddress address = new
EndpointAddress("http://www.dummygunk.com/Service1.svc");
Service1Client serviceClient = new Service1Client(binding, address);
Addendum 3/14/2014: My reference to C# 4.5 should really be a reference to the ASP.NET 4.5 Framework which uses C# 5.0.
No comments:
Post a Comment