【问题标题】:I'm having trouble launching my WCF service using a console application. What can be wrong?我在使用控制台应用程序启动 WCF 服务时遇到问题。有什么问题?
【发布时间】:2023-03-27 00:00:01
【问题描述】:

这是我的代码:

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 详情)。

【问题讨论】:

    标签: wcf console-application endpoint


    【解决方案1】:

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

    并回答您的问题。

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

    2) 您有两个配置端点的选项(reference)。相对和绝对。您这样做的方式将占用您的基础并附加您的亲戚 -> http://localhost:8000/Simple/BankService

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

    WCF ServiceHost access rights

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-06
      • 1970-01-01
      • 2012-05-23
      • 2023-03-25
      相关资源
      最近更新 更多