开发者

Can't Access SOAP Methods When Using Class Library

I am having problems accessing methods in my WCF class library and am wondering if somebody could explain why?

I have a web project that has a single DLL file - WebAPILibrary.DLL in its /bin folder. The SVC files in my Web Site are pointing to the corresponding contracts contained in the DLL file. So, for example, under the namespace WebProject.WebAPI.Auth I have:

   [DataContract]
    public class Auth
    {
        [DataMember]
        public string LoginUser;
    }


    [开发者_如何学JAVAServiceBehavior]
    public class AuthService : IAuthService
    {

     string IAuthService.LoginUser(string Email, string Password)
     {
            //do some stuff
     }

     }


    [ServiceContract(Namespace="WebAPI.Authentication")]
    public interface IAuthService
    {

        [OperationContract(Name = "LoginUser")]
        string LoginUser(string Email, string Password);

    }

Now, when I instantiate AuthService auth = new AuthService() in my web project, I would expect auth.LoginUser(string Email, string Password) but auth does not expose any of my defined methods. As a test, I instantiated IAuthService and get what I want. Since IAuthService is implemented by AuthService, shouldn't I be able to do the same by instantiating AuthService?

Also, when i point to the URL of any of my SVC files, they load as expected.

Thanks for your time.


When you "instantiate" IAuthService - probably by means of creating a client proxy for that service contract - you see the service contract view of the class, e.g. you see everything that's exposed on the [ServiceContract] as a [OperationContract] - the service methods. This is what gets exposed in your service description (the WSDL of your service) and it's totally irrelevant what visibility those methods have in .NET world.

However, if you directly instantiate the service class as a .NET class (and not via the WCF service runtime), then obviously, you're limited by the .NET visibility constraints, and members of a class are internal by default, e.g. not visible to the outside.

So basically, you need to distinguish whether you're looking at your class as a representation of a WCF service contract, or as a classic .NET class.

Marc

UPDATE: if you want to use your DLL "as a WCF service", you'll need to go the WCF client proxy way in order to use it. This can be done several ways:

  • create a client proxy using svcutil.exe from the command line
  • in Visual Studio, use "Add Service Reference" and let VS create your client proxy for you
  • or create the client proxy yourself, in code - not that hard to do really!

In code, you could do something like

ChannelFactory<IAuthService> factory = new ChannelFactory<IAuthService>();

IAuthService proxy = factory.CreateChannel();

Now, on your proxy class, you should see and be able to use your Login method.

To make all work, of course, you'll need the right set of config entries and all in your web.config.

Does that help?


You can slightly modify your code to the following

[ServiceBehavior]
public class AuthService : IAuthService
{    
 public string IAuthService.LoginUser(string Email, string Password)
 {
        //do some stuff
 }    
}

In your code, you have implemented the interface explicitly without making the access modifier public, therefore LoginUser method is visible only when the variable's declaring type is the specific interface. This is useful when you implement different interfaces with same member definition.

public interface ITest1
{
 void Hello();
}

public interface ITest2
{
 void Hello();
}

Explicit Impl

public class Impl : ITest1, ITest2
{
   void ITest1.Hello(){ ... }
   void ITest2.Hello(){ ... }
}

ITest1 obj1 = new Impl();
obj1.Hello() // this is calling ITest1.Hello()

ITest2 obj2 = new Impl();
obj2.Hello() // this is calling ITest2.Hello()

Or Implicit impl that only needs to have one member implemented.

public class Impl : ITest1, ITest2
{
   public void Hello(){ ... }
}


You should put "public" in front of "string IAuthService.LoginUser(string Email, string Password)" in your implementation, so it should be

public string IAuthService.LoginUser(string Email, string Password)
{
    // do some stuff
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜