Best way to manage multiple Service References in .NET

后端 未结 3 772
南笙
南笙 2021-02-06 18:45

I have a C# ASP.NET project with 15 WCF/ASMX Service References. Each service is deployed to three different servers; test, staging and live

3条回答
  •  长发绾君心
    2021-02-06 18:59

    First an overview

    The way I handle it is to not add the service refernce via Visual Studio. What I have is each service has it's own Proxy project in the solution, to proxy is created using a simple one line batch file. Each of these is then included in the website(s) as a "vanilla" reference. The end point is configured maunally in the web config. I have three deployment options: Debug (local), Staging and Release. At the web.config level the differnt addresses are handled by web.config transformations . The proxy project files are configured so that the correct end point address is used depending on the solution configuration. However it is important to note that web.config transofrmations only apply on publishing the solution.

    In pre VS2010 I had 3 variations of the web.config files which would overite the active web.config; however overwriting the web.config in this way always felt "risky" to me. In this case I think that using transforms for publish will still work, but in the actual web.config file have block of connections that you can comment in and out on those occations when you want to debug the staging or development servers.

    I only have 2 services, so my set up was pretty easy, with 15 there will be a fair bit of work involved to set up, but it will save you head aches in the long run.

    Implementation

    Back Up Everything First!!

    Also keep handy a copy of the existing web.config to help with configuring the end points later on.

    Also note that the batch files won't work if your solution is in a path with a space in its name, e.g. the default location VS puts its' projects. All my projects are in the following structure C:\Source\vs2008\, C:\Source\vs2010\ etc. I'll see if I can find a better solution for this.

    0 Run VisualStudio as an administrator

    Right click Visual Studio from the start menu and select "Run as Administrator". We need to do this as VS will be writing files.

    1 Delete Exising service references

    You shouldn't need any help on this one.

    2 Add Proxy Projects

    Doing this now means you only need configure the solution for test|staging|live once.

    From the File menu select "Add" then "New Project" and from unser the "Visual C#" tmplates select "Class Library" and name it something sensible. I'll be using FooService.proxy for this example. Rename the class file, I'll be using FooService_proxy.cs in this example.

    Add the following references to the project:

    • System.Runtime.Serialization
    • System.ServiceModel

    Add one project for each service. We will comeback and update these projects later.

    3 Configure The Solution to handle test|staging|live

    I'm assuming you use test when developing the asp.net website on your local machine.

    Open the "Configuration Manager" by selecting it in the Build Configuration dropdown menu.

    Under the "Active Solution Configuration" dropdown select "New"

    For the Name I'd suggest "Staging" and check the "Create new project confogurations" check box.

    In the Solution Explorer, right click on Web.Config and select "Add config transforms". This will add a new file for the staging web.config transformation. Click the expander arrow and you will see three sub files: Web.Debug.Config, Web.Release.Config, Web.Staging Config.

    4 Set Up Proxies

    Add a batch file to each proxy project by Clicking on the project in the solution explorer and selecting "Add > New Item". Use a text file and name it "CreateProxy.bat.

    Insert the following into the new file and save:

    :: ============================================================================================
    :: Create the proxy file from the service wsdl
    :: Input parameters
    :: SDK Path The location of svcutil.exe
    :: WSDL File Arg1 (%1)
    :: Output Proxy .CS file Arg2 (%2)
    ::
    :: Called by the build process of the BeforeBuild target to re-gen the proxy code.
    :: Make sure to change FooService.proxy
    :: ============================================================================================
    
    svcutil %1 /ct:System.Collections.Generic.List`1 /serializer:DataContractSerializer  /tcv:Version35  /n:*,FooService.Proxy /out:%2
    

    Now right click on the proxy project and click "Unload Project", saving if prompted. This will enable us to get in and modify the project file. Right Click the now greyed out proxy project name and select "Edit".

    Add the following just befor the closing tag. Note you may need to change the path SDKPath depending on your location of svcutil. Also make sure to name FooService_proxy.cs whatever you named the proxy file.

    
      
    
      C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin
      http://[Path to TEST Server Service]
      http://[Path to STAGING server Service]
      http://[Path to LIVE Server Service]
      false
    
    
      
      
    
    

    Save the changes and then right click on the greyed out project name and select "Reload Project".

    Right click on the project and select build, make sure that your proxy file is now populated.

    Set each proxy project so that it always build into only its' \bin directory, e.g. not bin\debug etc. Right click on the proxy project and select "Properties". Select the "Build" tab and change the "Configuration" drop down to "All Configurations". Set the "Output path" to bin\

    5 Add Proxy References and End Points To Your Website

    Add a reference to each proxy project to your webstie by right clicking "References > Add reference" and then going to the "Projects" Tab.

    Now open up your web.config file and add the bindings, use your previous web.config as a guide.

    
        
            
                
                    
                    
                        
                        
                    
                
                
            
        
        
            
            
            
    
            
    
            
        
    
    

    Now you can simply modify the web config with comments depending on which server you want to be debuging on

    6 Set up web.config transformation for deployment

    Expand the web.config node in the solution explorer.

    Open the web.staging.config file and add the following:

    
        
            
        
        
    
    

    Now add the same to Web.Release.Config changing the paths to tho the LIVE server paths. This will now use the appropriate endpoints when published using the VisualStudio publish command.

    E.g. If deploying a STAGING version of the web site, select "Staging" from the Build Configuraion drop down. Right click on the WebSite project in the solution expolorer and select "Publish". Choose how you wish to publish and click the "Publish" button. The whole solution will then rebuild, proxies will be generated form the Staging server and the web.config file will be published with the Staging setting.

    Thats it, you're done

    You now have proxies that will generate based on your build configuration, one location to change paths via commenting for debugging purposes, and automatic web.config updating on publising.

    Update

    Gaui, the OP, has created a small exe that simplifies this some what. It is availavle on GitHub

提交回复
热议问题