WCF, Typed Datasets and DataContracts

M

matthew-andrews

Hi,

I'm new to this WCF world, and have attempted to create a WCF service
which takes a typed dataset as its data contract. I know this isn't
always put forward for interopability, but I'm working in a pure .Net
environment (.Net 3.0) and VS2005.

However, how do I get the client to populate this data object to send
into the WCF service? After I generate the proxy class using svcutil
and do;

TypedDataSet ds = new TypedDataSet();

The 'ds' object isn't what I'd expect from a dataset! So how'd I get
data into it?!??!

Sorry if vague...

Alternatively any good web sites on WCF and data contracts?

Cheers
 
M

Marc Gravell

If you are working in pure .NET, and have control of both ends (and
are just uwing WCF as a comms layer, rather than an interopability
layer), then a sensible option is assembly sharing; put your contracts
(service and data) into a single assembly, and reference that assembly
from both the service (often running in IIS) and the client (generally
an exe). Now you don't need to use svcutil at all! You just configure
the endpoint in the config file, and use it. Both ends have exactly
the same interpretation of what the data is, and it "just works".

This also means you can have sensible business logic in your entity at
the client, rather than just the raw proxy.

Marc
 
M

Marc Gravell

I forgot to say... to represent the service, you can subclass
ClientBase<T> (which is what svcutil does); but using generics you
only have to do it once (obviously you need to add some other ctors
etc if you want to use non-default endpoints):


public sealed class WcfClient<T> : System.ServiceModel.ClientBase<T>,
IDisposable where T : class
{
void IDisposable.Dispose() { Dispose(); } // re-implement dispose
public void Dispose()
{
try
{// faulted state is poorly implemented by MS, and blocks
Close() and Dispose()!
switch (State)
{
case CommunicationState.Opened:
case CommunicationState.Opening:
Close(); break;
case CommunicationState.Faulted:
Abort(); break;
}
}
catch { }
}
public T Service
{
get { return base.Channel; }
}
}
[ServiceContract]
interface IFoo
{
[OperationContract]
void Bar();
}
static class Program {
static void Main()
{
// use our WCF service
using (WcfClient<IFoo> client = new WcfClient<IFoo>())
{
client.Service.Bar();
}
}
}
 
M

matthew-andrews

I forgot to say... to represent the service, you can subclass
ClientBase<T> (which is what svcutil does); but using generics you
only have to do it once (obviously you need to add some other ctors
etc if you want to use non-default endpoints):

public sealed class WcfClient<T> : System.ServiceModel.ClientBase<T>,
IDisposable where T : class
{
void IDisposable.Dispose() { Dispose(); } // re-implement dispose
public void Dispose()
{
try
{// faulted state is poorly implemented by MS, and blocks
Close() and Dispose()!
switch (State)
{
case CommunicationState.Opened:
case CommunicationState.Opening:
Close(); break;
case CommunicationState.Faulted:
Abort(); break;
}
}
catch { }
}
public T Service
{
get { return base.Channel; }
}}

[ServiceContract]
interface IFoo
{
[OperationContract]
void Bar();}

static class Program {
static void Main()
{
// use our WCF service
using (WcfClient<IFoo> client = new WcfClient<IFoo>())
{
client.Service.Bar();
}
}

}

Marc,

Many thanks this is exactly what I was looking for, it works a
treat.

Cheers
Mat
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top