Share Class between Windows app and ASP.NET website

L

Looch

All,

I created a class in a windows application and I'd like to be able to
pass an instance of that class as a method parameter to a web service.
The class contains 100 variables. I tried adding a new class to the
web service (using VS Web Developer Express) and recreated the
variables to match what was in the windows application class but when
I try to pass an object as a parameter I'm getting a 'Cannot
implicitly convert from winform.type to webservice.type'.

Is there an *easy* way to share a class between my win form app and
the web service so I can pass objects back and forth?

Thanks in advance.
 
M

Marc Gravell

Well, normally the proxy will create an entirely unrelated, meta-data
based type. With WCF there are a few tricks you can pull to share the
type more directly (i.e. without using svcutil.exe, which would do the
same thing as wsdl.exe) - but otherwise I think you have to stick to
treating the metadata version as just that: raw data.

You could perhaps write a utility method to convert between the two?
By copying the properties (either by hand, or by using some tricks
like reflection to do it more gracefully) or possibly (less nice) by
serialization / deserialization. Actually, you might even be able to
add cast operators to your main class (casting to the proxy class)
[not sure if this is a great idea]

Marc
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

If you put that class in a shared dll then both projects can have a
reference to it. Inaddition if the class support a Constructor or a method
that receive all the variables then either program can create an instance
that is the same in both programs. The only thing to note though is that you
will have two instances that will be the same at that point but that can be
modified independently afterwards.

Also take a look at Serialization, it could also solve your problem.
 
B

Bjorn Sigurd Johansen

Dear all,

I have a similar problem in a Visual Studio solution with two projects:

Project 1: "Normal" class library which contains public business objects. The DLL created by this project may be called directly by the user (as a DLL assembly).

Project 2: A WCF server containing services where I want to use the *SAME* business objects as parameters in public "ServiceContract" functions. (This is important as I do not want to write 2 versions of each business object).

But this seems somewhat impossible, as
1) It seems impossible to add the "DataContract"/"DateMember" attributes to objects in BusinessServer.dll, and also
2) It seems impossible to define the business objects in the WCF-server project and have them accessible/exposable by Project 1.

So, HOW TO REUSE BUSINESS OBJECTS BOTH AS "NORMAL" BUSINESS OBJECTS IN THE DLL, *AND* AS "DataContract" OBJECTS IN WCF SERVICE?

This type of code reuse seems to me to be very important.

Best regards,
Bjørn Sigurd Johansen
Norway
 

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