使用WCF测试客户端在Visual Studio中运行WCF服务时出现NullReferenceException

加布斯费雷拉

我使用WCF类库模板创建了一个非常简单的项目,现在我正在尝试在Visual Studio中运行它。没有编译错误,但是当WCF测试客户端窗口打开时,出现带有异常的对话框,并且我不知道发生了什么:

对话

这是该异常的完整描述:

************** Exception Text **************
System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.Tools.Common.SdkPathUtility.GetRegistryValue(String registryPath, String registryValueName)
   at Microsoft.Tools.Common.SdkPathUtility.GetSdkPath(Version targetFrameworkVersion)
   at Microsoft.Tools.TestClient.ToolingEnvironment.get_MetadataTool()
   at Microsoft.Tools.TestClient.ServiceAnalyzer.GenerateProxyAndConfig(String projectPath, String address, String configPath, String proxyPath, Int32 startProgressPosition, Int32 endProgressPostition, BackgroundWorker addServiceWorker, String& errorMessage)
   at Microsoft.Tools.TestClient.ServiceAnalyzer.AnalyzeService(String address, BackgroundWorker addServiceWorker, Single startProgress, Single progressRange, String& errorMessage)
   at Microsoft.Tools.TestClient.Workspace.AddServiceProject(String endpoint, BackgroundWorker addServiceWorker, Single startProgress, Single progressRange, String& error)
   at Microsoft.Tools.TestClient.AddServiceExecutor.Execute(AddServiceInputs inputs, Workspace workspace, BackgroundWorker addServiceWorker)
   at Microsoft.Tools.TestClient.UI.MainForm.addServiceWorker_DoWork(Object sender, DoWorkEventArgs e)
   at System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventArgs e)
   at System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object argument)


************** Loaded Assemblies **************
mscorlib
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.34209 built by: FX452RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll
----------------------------------------
WcfTestClient
    Assembly Version: 12.0.0.0
    Win32 Version: 12.0.21005.1 built by: REL
    CodeBase: file:///C:/Program%20Files%20(x86)/Microsoft%20Visual%20Studio%2012.0/Common7/IDE/WcfTestClient.exe
----------------------------------------
System
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.34239 built by: FX452RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Windows.Forms
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.34209 built by: FX452RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System.Drawing
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.34209 built by: FX452RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System.Configuration
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.34209 built by: FX452RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Configuration/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Xml
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.34230 built by: FX452RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
System.ServiceModel
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.34230 built by: FX452RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.ServiceModel/v4.0_4.0.0.0__b77a5c561934e089/System.ServiceModel.dll
----------------------------------------
System.Core
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.34209 built by: FX452RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Core/v4.0_4.0.0.0__b77a5c561934e089/System.Core.dll
----------------------------------------

我的项目IProductService中只有两个文件:

namespace LayerNorthwindService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IProductService
    {
        [OperationContract]
        Product GetProduct(int id);

        [OperationContract]
        bool UpdateProduct(Product product, ref string message);

        // TODO: Add your service operations here
    }

    // Use a data contract as illustrated in the sample below to add composite types to service operations.
    // You can add XSD files into the project. After building the project, you can directly use the data types defined there, with the namespace "LayerNorthwindService.ContractType".
    [DataContract]
    public class Product
    {
        [DataMember]
        public int ProductID { get; set; }
        [DataMember]
        public string ProductName { get; set; }
        [DataMember]
        public string QuantityPerUnit { get; set; }
        [DataMember]
        public decimal UnitPrice { get; set; }
        [DataMember]
        public bool Discontinued { get; set; }
    }
}

和ProductService:

namespace LayerNorthwindService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in both code and config file together.
    public class ProductService : IProductService
    {
        public Product GetProduct(int id)
        {
            var product = new Product();
            product.ProductID = id;
            product.ProductName = "Product name";
            product.UnitPrice = 10.0m;
            product.QuantityPerUnit = "fake QPU";
            return product;
        }

        public bool UpdateProduct(Product product, ref string message)
        {
            var result = true;

            if (product.UnitPrice <= 0)
            {
                message = "Prince cannot be <= 0";
                result = false;
            }
            else if (string.IsNullOrEmpty(product.ProductName))
            {
                message = "product name cannot be empty";
                result = false;
            }
            else if (string.IsNullOrEmpty(product.QuantityPerUnit))
            {
                message = "QPU cannot be empty";
                result = false;
            }
            else
            {
                message = "product updated successfully";
                result = true;
            }

            return result;
        }
    }
}
加布斯费雷拉

我通过执行此问题的可接受答案解决我的问题:复制到另一台计算机时无法在WcfTestClient中添加服务

在使用Windows 8.1时,我已经安装了Windows 8.1的Windows SDK,现在一切正常。

如果要在计算机上安装它,请注意要安装的SDK版本。每个Windows版本都有一个正确的版本。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在客户端测试中测试WCF时出错

使用Visual Studio 2012构建后出现WCF ODATA客户端错误

如何通过WCF测试客户端访问联合WCF?

无法调用该服务,WCF测试客户端?

WCF 测试客户端,调用服务失败

如何使用客户端中的接口注入WCF服务?

单元测试-如何使用MOQ进行MOQ WCF客户端

对WCF客户端进行单元测试

在WCF客户端中调用某些服务时出错

使用WCF客户端+ Cookie进行Web服务调用

WCF客户端使用WS-Security Web服务

从WCF测试客户端调用WebBrower控件无法在自托管WCF服务中导航

以编程方式让客户端应用程序列出运行时使用URL创建的WCF服务的端点(如WCF测试客户端所做的那样)

将WCF服务添加到WCF测试客户端时出错-无法访问服务元数据

客户端在远程地址中使用此Web服务后,在本地项目上测试WCF Web服务

使用 wsDualHttpBinding 在 WCF 中获取客户端 IP 地址

运行WCF测试客户端并在app.config中增加后收到MaxReceivedMessageSize错误

Wcf数据服务客户端

WCF服务PHP客户端连接

WCF 在客户端调用服务

使用统一注入WCF代理客户端

运行 winforms 客户端,不使用引用的 wcf 服务启动 iisexpress - 在 Windows 10 下。想法?

在 WCF 客户端中填充 XmlAnyAttribute

错误:WCF测试客户端不支持,因为它使用类型System.Threading.Tasks

单元测试服务和模拟自动生成的WCF客户端代理

客户端中的WCF服务通过https返回404

使用WS安全性的Axis2 / Rampart Web服务的WCF客户端,无需客户端证书

客户端脱机时,可以使用WCF双工服务来呼叫客户端吗?

如何使用WCF在C#中调试Localhost客户端/服务