How to add a web service reference in a DLL

后端 未结 5 1076
走了就别回头了
走了就别回头了 2021-01-03 00:07

I\'m creating a DLL with a reference to web services (I don\'t have the choice to do so) but I have to add web service references to the project that uses the DLL for it to

5条回答
  •  青春惊慌失措
    2021-01-03 00:35

    Your first step is to prove to yourself that this is possible and then adapt your project.

    you can download the runnable solution here.

    I just went through the steps and enumerated my actions to produce the results you desire.

        Create a web app project (an thus a solution) named 'ReferencedWebService'
        Add a web service, just leave the default name and implementation
        Add a class library named 'ReferencedWebserviceAPI'
            Add Service Reference
                >Advanced
                    >Add Web Reference>Web services in this solution
                        >WebService1
                            >Add reference leaving name as 'localhost'
        Add a console application project named 'ReferencedWebserviceClient'    
        To ReferencedWebserviceClient: 
            Add Reference
                >Projects
                    >ReferencedWebserviceAPI
            Add Reference
                >.Net
                    >System.Web.Services
    
        In Program.cs replace Main:
    
        static void Main(string[] args)
        {
            var svc = new ReferencedWebserviceAPI.localhost.WebService1();
            var result = svc.HelloWorld();
            Console.WriteLine(result);
            Console.ReadLine();
        }
    
    
        Set ReferencedWebserviceClient as startup project and run.
    
        Ok, this is simplest scenario. One issue you will have to deal with is that the default Service URL is hardcoded in the .dll,
        and it is set to a ported address on your dev machine.
    
        You will want to add a configuration parameter to your client project. The simplest and most portable way is to use an appSetting.
    
        To ReferencedWebserviceClient:
            Add Item
                >Application Configuration File
    
        Replace contents of App.Config with this, of course replace the value with the appropriate value on your machine.
    
        
        
          
            
          
        
    
    
            Add Reference
                >.Net
                    >System.Configuration
    
        Now replace Main with this:
    
        static void Main(string[] args)
        {
            var svc = new ReferencedWebserviceAPI.localhost.WebService1
                          {
                              Url = ConfigurationManager.AppSettings["serviceUrl"]
                          };
            var result = svc.HelloWorld();
            Console.WriteLine(result);
            Console.ReadLine();
        }
    

    This is the baseline for embedding services in a redistributable .dll.

    Try to apply this model to your current project and see how that works for you.

    If you still have problems, you most definitely have a reference issue and should start looking at it from that perspective.

    Hope this helps

提交回复
热议问题