如何在设计时调用服务而不添加服务引用 [英] How to call a service without adding service reference at design time

查看:110
本文介绍了如何在设计时调用服务而不添加服务引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。

我试图调用使用.net的c#和WCF项目写的服务。

主机的位置不稳定和IP将随时改变时间。我需要在不向客户端添加服务引用的情况下调用服务。

我发现了这个如何使您的Web引用代理URL动态化 [ ^ ]文章,但是当我发布我的客户端时,我没有任何配置文件来更改其中的URL。

任何人都可以帮我找到如何发布我的客户端,该客户端也将包含.config文件,或者通常在VS中发布项目的适当方式是什么。

最好的问候

Hi every one
I am trying to call a service written using c# and WCF project of .net .
The location of the host is not stable and the IP will change time by time. I need to call the service without adding the service reference to the client.
I found thisHow to make your Web Reference proxy URL dynamic[^] article from code project but when I publish my client I do not have any ,config file to change the url in it.
Can anyone help me to find how to publish my client which will also contain a .config file or generally what is the appropriate way to publish a project in VS.
best regards

推荐答案

你肯定会有app.config或web.config,因为它需要指定终点连接



First Create一个ConfigHelper来编辑地址



You will definitely have app.config or web.config as it is required to specify the end point connections

First Create a ConfigHelper to Edit the address

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Reflection;



    public class ConfigHelper
    {
        private static string NodePath = "//system.serviceModel//client//endpoint";
        private ConfigHelper() { }

        public static string GetEndpointAddress()
        {
            return ConfigHelper.loadConfigDocument().SelectSingleNode(NodePath).Attributes["address"].Value;
        }

        public static void SaveEndpointAddress(string endpointAddress)
        {
            // load config document for current assembly
            XmlDocument doc = loadConfigDocument();

            // retrieve appSettings node
            XmlNode node = doc.SelectSingleNode(NodePath);

            if (node == null)
                throw new InvalidOperationException("Error. Could not find endpoint node in config file.");

            try
            {
                // select the 'add' element that contains the key
                //XmlElement elem = (XmlElement)node.SelectSingleNode(string.Format("//add[@key='{0}']", key));
                node.Attributes["address"].Value = endpointAddress;

                doc.Save(getConfigFilePath());
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static XmlDocument loadConfigDocument()
        {
            XmlDocument doc = null;
            try
            {
                doc = new XmlDocument();
                doc.Load(getConfigFilePath());
                return doc;
            }
            catch (System.IO.FileNotFoundException e)
            {
                throw new Exception("No configuration file found.", e);
            }
        }

        private static string getConfigFilePath()
        {
            return Assembly.GetExecutingAssembly().Location + ".config";
        }
    }





然后创建一个继承客户类的Helper Class





Then create a "Helper Class" that inherits the client class

class WCFHelper : WCFClient
{
    public WCFClientHelper()
    {
        var endpointAddress = Endpoint.Address;

        EndpointAddressBuilder newEndpointAddress = new EndpointAddressBuilder(endpointAddress);
        newEndpointAddress.Uri = new Uri( ConfigHelper.GetEndpointAddress());
        this.Endpoint.Address = newEndpointAddress.ToEndpointAddress();
    }
}



只要您想使用服务而不是使用客户端类,请使用此帮助程序类




Use this helper class whenever you want to use the Service instead of using the client class

WCFHelper client = new WCFHelper();
     lstVenue.ItemsSource = client.GetSomething();
     client.Close();


我找到了一种简单的方法来做我想做的事。

我在互联网上发现了很多页面寻址.config文件但我无法找到该文件。

这就是我解决问题的方法:



在visual studio的项目解决方案中有一个.config文件。如果此文件中的地址正确,则发布的程序将不会查找任何其他文件(如.config文件)以获取服务的地址。所以我将项目解决方案中app.config文件中指定的地址更改为无意义的地址。

然后没有发布项目,我只按f6刷新项目的相关文件。 />
我移动到保存项目的路径。对我来说是:C:\ Users \ user \ Documents \ Visual Studio 2012 \Projects \ my project

然后我移动到bin目录然后调试目录

在这里,我找到了我的解决方案的.exe文件和.config文件,这是我正在寻找的正确文件。

我将.exe和.config文件复制到另一个目录中。现在,当我运行.exe文件时,它将查看我在其旁边复制的.config文件。如果正确定义了.config文件中的Web服务的地址,那么我可以从该客户端访问Web服务中的方法。



我希望它也会帮助其他寻求从外部文件寻址Web服务的人,而无需重新编译解决方案。
I found a simple way to do what I wanted to do.
I found many pages in the internet addressing the .config file but I was not able to find the file.
This is the way that I solved my problem:

there is a .config file in the project solution in the visual studio. If the address in this file is correct then the published program will not look for any other file such as .config file to get the address of the service. so I changed the address specified in the app.config file in the project solution to a nonsense address.
Then without publishing the project, I only pressed f6 to refresh the related file to the project.
I moved to the path where my project is saved. For me it is: C:\Users\user\Documents\Visual Studio 2012\Projects\my project
Then I moved to bin directory and then debug directory
Here I found the .exe file of my solution and a .config file which is the right file that I was looking for.
I copied the .exe and .config file in another directory. Now when I run the .exe file it will look to the .config file that I copied just beside it. If the address of the web service in the .config file is correctly defined then I can access the methods in the web service from this client.

I wish it will also help others who are looking for addressing web service from an external file without recompiling the solution.


这篇关于如何在设计时调用服务而不添加服务引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆