我在使用控制台应用程序启动 WCF 服务时遇到问题。有什么问题吗?

发布于 2024-08-29 05:05:32 字数 1866 浏览 7 评论 0原文

这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using BankServiceClient.BankServiceReference;

namespace BankServiceClient
{
    class Program
    {
        static void Main(string[] args)
        {
            Uri baseAddress = new Uri("http://localhost:8000/Simple");
            Type instanceType = typeof(BankServiceReference.BankClient);
            ServiceHost host = new ServiceHost(instanceType,baseAddress);

            using (host)
            {
                Type contractType = typeof(BankServiceReference.IBank);
                string relativeAddress = "BankService";
                host.AddServiceEndpoint(contractType, new BasicHttpBinding(), relativeAddress);

                host.Open();

                Console.WriteLine("Press <ENTER> to quit.");
                Console.ReadLine();

                host.Close();
            }

            /*
             * Consuming a WCF Service and using its method.
             */

            //IBank proxy = new BankClient();

            //double number = proxy.GetBalance(1234);

            //Console.WriteLine(number.ToString());
            //Console.ReadLine();
        }
    }
}

首先,有几个问题:

  1. “baseAddress”属性,它到底是什么?当我使用默认的 F5(无控制台应用程序)启动服务时,该服务在 localHost 上的随机端口上启动。我怎样才能写下一个准确的数字并期望它到达那里?对此感到困惑。

  2. relativeAddress 属性是什么?它说 BankService 但我应该在该属性中写什么?对此也感到困惑。

这是当我尝试运行此控制台应用程序时收到的确切错误消息:

HTTP 无法注册 URL http://+:8000/Simple/。您的流程 没有对此的访问权限 命名空间(参见 http://go.microsoft.com/fwlink/?LinkId=70353 详情)。

Here's my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using BankServiceClient.BankServiceReference;

namespace BankServiceClient
{
    class Program
    {
        static void Main(string[] args)
        {
            Uri baseAddress = new Uri("http://localhost:8000/Simple");
            Type instanceType = typeof(BankServiceReference.BankClient);
            ServiceHost host = new ServiceHost(instanceType,baseAddress);

            using (host)
            {
                Type contractType = typeof(BankServiceReference.IBank);
                string relativeAddress = "BankService";
                host.AddServiceEndpoint(contractType, new BasicHttpBinding(), relativeAddress);

                host.Open();

                Console.WriteLine("Press <ENTER> to quit.");
                Console.ReadLine();

                host.Close();
            }

            /*
             * Consuming a WCF Service and using its method.
             */

            //IBank proxy = new BankClient();

            //double number = proxy.GetBalance(1234);

            //Console.WriteLine(number.ToString());
            //Console.ReadLine();
        }
    }
}

First, a couple of questions:

  1. The 'baseAddress' attribute, what exactly is it? When I launched my service using the default F5 (no console application) the service launched on a random port on localHost. How can I write in an exact number and expect it to go there? Confused at this one.

  2. What is the relativeAddress attribute? It says BankService but what should I write in that attribute? Confused at this one as well.

Here's the exact error message I get when I try to run this Console application:

HTTP could not register URL
http://+:8000/Simple/. Your process
does not have access rights to this
namespace (see
http://go.microsoft.com/fwlink/?LinkId=70353
for details).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一杆小烟枪 2024-09-05 05:05:33

首先,您的客户项目是否设置为启动项目?

并回答您的问题。

1) baseAddress (URI 类) 是您托管的基地址服务。我认为您正在启动其他项目。

2) 您有两种配置端点的选项(参考)。相对和绝对。你这样做的方式将获取你的基地并附加你的亲戚 -> http://localhost:8000/Simple/BankService

最后要解决您的托管问题,请参阅此链接:

WCF ServiceHost 访问权限

First is your client project set to be the start up project?

And to answer your questions.

1) baseAddress (URI Class) is the base address for your hosted service. I am thinking you are launching some other project.

2) You have two options on configuring endpoints(reference). Relative and Absolute. The way you did it will take your base and appends your relative -> http://localhost:8000/Simple/BankService

Lastly to fix your hosting issue see this SO link:

WCF ServiceHost access rights

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文