0

我正在尝试将 excel 连接到 WCF 服务,但我似乎无法让一个微不足道的案例工作......当我尝试在 excel 中创建代理时出现无效的语法错误。我已将 Visual Studio 调试器附加到 Excel,并发现真正的错误是“找不到接口”。我知道该服务有效,因为由 Visual Studio 创建的测试客户端没问题……所以问题出在 VBA 名字对象字符串中。

我希望找到两件事之一:

1)对我的名字字符串进行更正以使这项工作正常进行,或者

2) 要下载的现有示例项目,该项目具有有效的主机和客户端的源代码。

这是我的 VBA 客户端的代码:

Dim addr As String
addr = "service:mexAddress=net.tcp://localhost:7891/Test/WcfService1/Service1/mex, "
addr = addr + "address=net.tcp://localhost:7891/Test/WcfService1/Service1/, "
addr = addr + "contract=IService1, contractNamespace=http://tempuri.org, "
addr = addr + "binding=NetTcpBinding_IService1, bindingNamespace=""http://tempuri.org"""

MsgBox (addr)

Dim service1 As Object
Set service1 = GetObject(addr)

MsgBox service1.Test(12)

我有以下服务:

[ServiceContract]
public interface IService1
{
    [OperationContract]
    string GetData(int value);
}

public class Service1 : IService1
{
    public string GetData(int value)
    {
        return string.Format("You entered: {0}", value);
    }
}

它有以下配置文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <system.web>
    <compilation debug="true" />
  </system.web>
  <!-- When deploying the service library project, the content of the config file must be added to the host's 
  app.config file. System.Configuration does not support config files for libraries. -->
  <system.serviceModel>
    <services>
      <service behaviorConfiguration="WcfService1.Service1Behavior"
        name="WcfService1.Service1">
        <endpoint address="" binding="netTcpBinding" bindingConfiguration=""
          contract="WcfService1.IService1">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexTcpBinding" bindingConfiguration=""
          contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:7891/Test/WcfService1/Service1/" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="WcfService1.Service1Behavior">
          <serviceMetadata httpGetEnabled="false" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

</configuration>

编辑:

对我有用的更新绰号如下

Dim addr As String
addr = "service:mexAddress=""net.tcp://localhost:7891/Test/WcfService1/Service1/Mex"", "
addr = addr + "address=""net.tcp://localhost:7891/Test/WcfService1/Service1/"", "
addr = addr + "contract=""IService1"", contractNamespace=""http://tempuri.org/"", "
addr = addr + "binding=""NetTcpBinding_IService1"", bindingNamespace=""http://tempuri.org/"""
4

2 回答 2

0

我会将客户端创建为 .NET 对象,将其注册为 COM 对象并通过 COM 从 VBA 访问它

更新:我发现这篇文章:http: //msdn.microsoft.com/en-us/library/ms752245.aspx

似乎要使用名字对象,您必须生成一个客户端,然后将其注册到 COM,然后使用它的 GUID 作为您的类型,而不是您拥有的类型

于 2010-06-03T19:11:59.747 回答
0

我可以通过在名字对象字符串上的一些关键位置添加一些引号来解决这个问题......不幸的是,将调试器附加到 Excel 给你的反馈不太有用,所以修复它是一个猜测和检查的练习。在 VBA 中,您需要在名字对象中的每个字符串周围加上双引号 ("")。此外,mex 地址中的术语“mex”必须大写,即使我在合同中将地址指定为小写。去搞清楚。

于 2010-06-07T18:57:32.873 回答