简体   繁体   中英

WCF Service can't return struct from another class?

So I am currently writing a new WCF Service, which should return a Struct when one of it's functions is called. The struct is held in a shared class since it is used in others areas of the program.

The struct looks as follows (Note, it is inside a VB.Net class, some of the project is in C#):

<DataContract()>
Public Structure WrapperResults
    <DataMember()>
    Dim Success As Boolean
    <DataMember()>
    Dim ErrorMessage As String
End Structure

Now in the WCF Service I have set up I have a simple test function that looks like this:

public class TFXEmailProcessor : Wrapper
    {
        public MQShared.Functions.WrapperResults CallWrapper(string AppName, string Password, string ConfigXML)
        {
            MQShared.Functions.WrapperResults results = new MQShared.Functions.WrapperResults();

            results.ErrorMessage = "TFX Test";
            results.Success = true;

            return results;
        }
    }

And in another class I have added a reference to my WCF Service and attempt to call it like:

Dim myBinding As New BasicHttpBinding()
Dim endpointAddress As New EndpointAddress(WP.MyWrapper(x).WrapperURL)
Dim SR As New WrapperService.WrapperClient(myBinding, endpointAddress)

Dim WrapResults As MQShared.Functions.WrapperResults = SR.CallWrapper(AppName, Password, WP.MyWrapper(x).ConfigXML)

However the SR.CallWrapper function is highlighted by Intellisense and I get the error Value of type 'FunctionsWrapperResults' cannot be converted to 'Functions.WrapperResults' (Note the missing period in FunctionsWrapperResults)

Is there something I am missing here?

Fixed this problem just by letting the compiler work out the return value instead of specifically declaring it as

Dim WrapResults As MQShared.Functions.WrapperResults

I now simply declare the function call as:

Dim WrapResults = SR.CallWrapper(AppName, Password, WP.MyWrapper(x).ConfigXML)

There are two ways to call WCF service. Proxy and Channel .

In Proxy, you can add WCF service to your project with Add Service Reference ... In this way, proxy classes generated automatically. You can't use your shared classes. Because, shared classes generated again as proxy.

If you can use shared classes, you must choose Channel way. In channel, add ServiceContact (interface) and DataContract to the client project.

I use C#

    var address = new EndpointAddress("..."); // Service address
    var binding = new BasicHttpBinding(); // Binding type

    var channel = ChannelFactory<IService>.CreateChannel(binding, address);

    MQShared.Functions.WrapperResults WrapResults = channel.CallWrapper(string AppName, string Password, string ConfigXML);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM