【问题标题】:NullReferenceException when running WCF service in Visual Studio using WCF Test Client使用 WCF 测试客户端在 Visual Studio 中运行 WCF 服务时出现 NullReferenceException
【发布时间】:2015-01-19 17:22:09
【问题描述】:

我使用 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; }
    }
}

和产品服务:

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;
        }
    }
}

【问题讨论】:

  • 想必你在服务代码中放了断点?这应该是最容易调试的事情之一。
  • 可能与stackoverflow.com/questions/15325867/… 相关?例外情况类似。
  • 以管理员权限打开 Visual Studio/WCF 客户端
  • @pmcoltrane 安装 de Windows 8 SDK 如该问题所述解决了我的问题。如果您愿意,请回答问题,以便我标记您的答案。
  • @bigodera 如果 pmcoltrane 没有添加答案,你应该自己做,以帮助未来用户遇到同样的问题

标签: c# visual-studio wcf


【解决方案1】:

在尝试从注册表读取信息时,“WCF 测试客户端”中似乎存在错误。

如果查看源代码,则实际上可以不安装 SDK。但是这个错误阻止了该工具实际使用在访问注册表失败时运行的代码,因为 NullReferenceException 不是预期的,但它应该是。在不存在的键上调用 OpenSubKey 会返回 null,因此在通过 GetValue 访问时会失败。

// Microsoft.Tools.Common.SdkPathUtility
private static string GetRegistryValue(string registryPath, string registryValueName)
{
    string result;
    try
    {
        using (RegistryKey registryKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32))
        {
            using (RegistryKey registryKey2 = registryKey.OpenSubKey(registryPath, false))
            {
                string text = registryKey2.GetValue(registryValueName, string.Empty) as string;
                result = text;
            }
        }
    }
    catch (UnauthorizedAccessException)
    {
        result = string.Empty;
    }
    catch (SecurityException)
    {
        result = string.Empty;
    }
    return result;
}

那么应该如何向微软报告呢?

【讨论】:

    【解决方案2】:

    我解决了我的问题,按照这个问题的公认答案:Unable to add service in WcfTestClient when copy to a different machine

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

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

    【讨论】:

    • 我安装了所有的SDK,但仍然出现这个错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-05
    • 1970-01-01
    • 2011-11-28
    • 2011-07-10
    • 2016-06-05
    • 1970-01-01
    相关资源
    最近更新 更多