开发者

Create a reusable stub for multiple webservices (ASMX)

I'm having a couple of websites hosted different places, which I want to backup regularly.

However I only need the user-uploaded files, so I've made a simple Web service on each site, with the same definition:

public class BackupService : System.Web.Services.WebService
{
    [WebMethod]
    public string[] GetFileList()
    {
        // Get list from DB
    }
}

The namespace is obviously different, however I think it should be possible to use a single stub for this, and just call that stub with different URL开发者_JAVA技巧s.

The problem is, I can't seem to find any examples of how to do this - so either it's not possible (which I doubt until proven wrong) or I just suck at searching.

Can anyone come up with an idea of how to do this ?

The reason I want to do it, is I regularly add new websites to my "backup list", and I don't want to recompile my backup software each time. Currently I don't backup the files, I just get the databases, and these I've just defined in an XML document, so each new website is just an entry in the XML.

My hope is that it's possible to do the same with the webservices, so I can just add the ASMX url in my XML file, and thus avoid recompiling.


So what's the problem with deploying that web service to the various hosted (and different physical locations) and changing the web service endpoint URL in your SOAP client from your calling code?

Or am I missing the point of your question?

OK, having read your comment, here's a few more pointers.

All that Add Web Reference does, is, it generates a SOAP proxy client in your Reference.cs file. Because the contract for these is all the same, you only need to do this once.

Then, where you set use your SOAP client proxy in your calling code, set the Url to the appropriate .asmx endpoint (load it from your custom XML config or web.config):

YourWebServiceClient client = new YourWebServiceClient();
client.Url = "http://server/webservice/yourendpoint.asmx"; // get from config
string[] filenames = client.GetFileList();

That's all you should need to do. Hope it helps.

Also - ensure that for the different webservices, you don't change the webservice namespace. Keep it the same. It may be more convenient to use a URN namespace, rather than a URI, as it's tempting to change the namespace with the URI to stay in line with the URL of the webservice, however this is not necessary.

So for instance, mark your webservice up using a URN namespace:

[WebService(Namespace = "urn:your:mywebservice")]

And deploy that same webservice to the various locations.


As an alternative, you could have a static class with the logic that the web service uses, and call that in each:

public class BackupService : System.Web.Services.WebService { [WebMethod] public string[] GetFileList() { return BackupHelper.GetFileList(); } }

public static class BackupHelper { public string[] GetFileList() { .. } }

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜