473,394 Members | 1,737 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Downcasting from Interface on WCF Proxy

I have created a basic WCF web service that returns a MessageContract class which has a member which is an interface called IOutcome (marked up as MessageBodyMember). There are two classes that implement IOutcome which are SuccessfulOutcome and FailureOutcome. FailureOutcome has a Property on it called ErrorDetails. Both of these subclasses are marked up as DataContracts and the members are marked as DataMembers, but the IOutcome class couldn't be marked up as [DataContract] without getting errors.

When I generate a proxy on the client application to call this service the FailureOutcome and SuccessfulOutcome classes do not get generated, so although I can refer to members of IOutcome, I can't get to the ErrorDetails on FailureOutcome as I don't have the type to downcast it to.

Am I coming at this problem from the wrong angle? It feels like it.

Initially I just wanted to return ErrorDetails on an exception but I couldn't figure out how to return a custom exception without it being turned into a FaultException and hence losing the custom fields. I suspect that FaultContract might help with this issue but I don't know much about that.

Suggestions on either of these approaches would be gratefully appreciated.
Jan 13 '09 #1
7 5292
mldisibio
190 Expert 100+
Would something like this help?

Expand|Select|Wrap|Line Numbers
  1. IOutcome outcome = getOutcomeFromProxy();
  2. FailureOutcome failure = outcome as FailureOutcome;
  3. if(failure != null){
  4.   // handle failures
  5. }
  6. else{
  7.   SuccessfulOutcome success = outcome as SuccessfulOutcome;
  8.   if(success != null){
  9.     // handle success
  10.   }
  11.   else{
  12.     throw new InvalidOperationException("Return type unknown.");
  13.   }
  14. }
Jan 13 '09 #2
Thanks for your reply, but unfortunately it doesn't offer me any help, but it does illustrate my point quite nicely. That is basically the code that I was trying to write, but my problem is that it won't compile as the types FailureOutcome and SuccessfulOutcome don't exist in my client solution, only in the service. I would have expected them to be generated as part of the proxy that appears when adding the reference but they don't. Surely all types in a MessageContract should get generated if they are correctly marked up with DataContract, DataMember etc.
Jan 13 '09 #3
mldisibio
190 Expert 100+
Ah, I see. I am currently working on a proxy/server application and have run across the same issue.
If you only publish the interface, but your client wants to cast to the implementation, an exception will be thrown demanding a reference to the assembly containing the definition of the implementation as well.

One solution I can offer is not very elegant, but works:

In addition to (or in place of) the interface (IOutcome), you could publish the implementations (SuccessfulOutcome and FailureOutcome) as abstract classes to the public API which the client sees.

Your server would have actual implementations of the abstract classes. Your proxy would then return a copy of the server classes cast to the public abstract representation.

However, you will run across the exact same issue unless you implement a cloning method or a method or copy constructor in which each derived class on the server (SuccessfulOutcome for example) can COPY itself as an instance of the abstract class. This is tricky, because you cannot create an instance of an abstract class, but the copy can be done.

The other option is simply to have two concrete classes which both implement the IOutcome interface. One is published to the client with minimum implementation, perhaps holding only data; the other is internal to your server and would have the implementation details you do not want the client to have. If they are concrete classes, then the server can create a new instance of the client version, copy the client-only details to it, and serialize it through the proxy.
Jan 13 '09 #4
mldisibio
190 Expert 100+
Wait - on closer reading of your post, my reply may be referring to a different client/proxy scenario (Remoting, not WebService).
Explain what protocol you are using and how you are "generating the proxy."
Jan 13 '09 #5
This is a WCF web service that I am calling from a windows application. I generate the proxy using Visual Studio 2008 and going to the SolutionExplorer, then under my project name right click on Service References, select Add Service Reference then using the form that appears I paste in the URL for the web service, tweak a couple of minor settings then click OK. The new service proxy then appears under Service References in the Solution Explorer. I expect it does much the same as using svcutil.exe which I used to use with older versions of Visual Studio.
Jan 14 '09 #6
mldisibio
190 Expert 100+
I see what you are doing and I understand the issue. Unfortunately, I haven't worked with the WCF Proxy/Serialization model. (I thought the names DataContract etc were your own class names). However, the overall proxy architecture is the same for different implementations, and the fundamental issue is still "how a client can cast to a specific implementation when the proxy only publishes an Interface?" - a common issue.

There might be someway to make your model work using WCF syntax/attributes, and if someone else sees the exact fix, I hope they jump in. Feel free to re-post your question to the forum, since I haven't helped much.

Nonetheless, I can suggest a workaround: Can you not add ErrorDetails to IOutcome, along with a boolean "HasErrors". This is the model of the AsyncCompletedEventArgs class. You check the "Error" property, and if it is null, you can check the "Result" property. If it is not null, you know your operation failed.

So a workaround could be essentially to make the Success/Failure Outcome classes into one.
Jan 15 '09 #7
I think that may well be the way I have to go with it. I'm sure that way will work fine, just don't like to design code to avoid things I can't figure out. Oh well. Thanks for all your help anyway, much appreciated.
Jan 15 '09 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Stuart Golodetz | last post by:
Hi, I've got a minor casting issue which I need to check about (marked // <--). I was trying to do a static_cast on it (which didn't work, though I'm not sure why this should be the case?) I...
2
by: sys | last post by:
I have a Web service and I want it to implement an interface that I specified, say IService. So my Web service class looks like this public class Service1 : System.Web.Services.WebService, IServic...
2
by: Derrick | last post by:
Is there an implement interface wizard is C#/Visual Studio? And, I have an interface, with many implentations, want to provide a Web Service implementation. What would be the standard practice? ...
5
by: HenrySeque | last post by:
I have a webservice and I add a web reference from my web project. I want the proxy class to implements an Interface, but I didn't find the source code of the proxy class and I don't want to...
1
by: Mike9900 | last post by:
I want to use an interface in both the webservice and its client. For example, the web service implements an interface and then the client cast that webservice to the inteface. It seems impossible,...
1
by: WTH | last post by:
I want to keep the codebase simple and deployment simple as well, so I'd like to expose multiple interfaces to web services consumers from the same assembly. This wasn't possible a year ago...
5
by: ayaz.usman | last post by:
Hi, I've built a web services proxy server, in C# using wsdl.exe, by importing wsdl. Howeever, when I go to : http://localhost/sample.asmx?wsdl, they wsdl there does not match the wsdl I fed...
5
by: Ryan van Roode | last post by:
Hello, An API function gives me an object of class 'Blah'. I have subclassed 'Blah' and added a few methods: class EnhancedBlah extends Blah { function enhancement1() {} function...
4
by: -Lost | last post by:
How should one write a plug-in interface? I've tossed around several ideas but rudimentary ones at best. For example: Plug-In-A -Plug-In-Proxy -Application The plug-in simply hands off its...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.