5

有一个带有插件的应用程序,它以编程方式生成 WCF 客户端合同,然后将其连接到插件接口,但是我正在努力弄清楚如何让生成的合同重用插件 dll 中的类型。

有谁知道如何设置 ServiceContractGenerator 以重用已定义程序集中的类型?

这是我用来生成合约代码atm的:

        public Assembly CreateProxy(String url)
    {
        MetadataExchangeClient mexClient = new MetadataExchangeClient(new Uri(url + "/mex"), MetadataExchangeClientMode.MetadataExchange);
        mexClient.ResolveMetadataReferences = true;

        MetadataSet metaDocs = mexClient.GetMetadata();
        WsdlImporter importer = new WsdlImporter(metaDocs);

        ServiceContractGenerator generator = new ServiceContractGenerator();

        generator.NamespaceMappings.Add("*", "NameSpace123");

        Collection<ContractDescription> contracts = importer.ImportAllContracts();
        ServiceEndpointCollection endpoints = importer.ImportAllEndpoints();

        foreach (ContractDescription contract in contracts)
            generator.GenerateServiceContractType(contract);

        if (generator.Errors.Count != 0)
            throw new Exception("There were errors during code compilation.");

        CodeDomProvider codeDomProvider = CodeDomProvider.CreateProvider("C#");
        CompilerParameters parameters = new CompilerParameters();

        parameters.CompilerOptions = string.Format(@" /lib:{0}", "\"C:\\Program Files\\Reference Assemblies\\Microsoft\\Framework\\v3.0\"");
        parameters.ReferencedAssemblies.Add("System.ServiceModel.dll");
        parameters.ReferencedAssemblies.Add("System.Runtime.Serialization.dll");

        parameters.GenerateExecutable = false;
        parameters.GenerateInMemory = true;
        parameters.IncludeDebugInformation = true;
        parameters.OutputAssembly = "WCFGenerated.dll";

        CodeCompileUnit codeUnit = generator.TargetCompileUnit;
        CompilerResults results = codeDomProvider.CompileAssemblyFromDom(parameters, codeUnit);

        foreach (CompilerError oops in results.Errors)
            throw new Exception("Compilation Error Creating Assembly: " + oops.ErrorText);

        //Must load it like this otherwise the assembly wont match the one used for the generated code below
        return Assembly.LoadFile(Directory.GetCurrentDirectory() + "\\WCFGenerated.dll");
    }

编辑:我从来没有让它工作得很好,但是我确实设法将 exe 作为程序集加载并使用它来生成代理:

        try
        {
            Thread.CurrentThread.CurrentUICulture = CultureInfo.CurrentUICulture.GetConsoleFallbackUICulture();
            if (Console.OutputEncoding.CodePage != Encoding.UTF8.CodePage && Console.OutputEncoding.CodePage != Thread.CurrentThread.CurrentUICulture.TextInfo.OEMCodePage)
            {
                Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
            }

            var assembly = Assembly.LoadFile(Path.Combine(info.TempDir, SVCUTIL_EXE));

            var optionsType = assembly.GetType("Microsoft.Tools.ServiceModel.SvcUtil.Options");
            var runtimeType = assembly.GetType("Microsoft.Tools.ServiceModel.SvcUtil.ToolRuntime");

            //Options option = Options.ParseArguments(args);
            var options = optionsType.InvokeMember("ParseArguments", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.InvokeMethod, null, null, new object[] { info.Args });

            //ToolRuntime toolRuntime = new ToolRuntime(option);
            ConstructorInfo c = runtimeType.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] { optionsType }, null);
            var runtime = c.Invoke(new Object[] { options });

            //var runtime = Activator.CreateInstance(runtimeType, , null, options);

            //toolRuntime.Run();
            var exitCode = (int)runtimeType.InvokeMember("Run", BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance, null, runtime, null);

            if (exitCode != 0)
                throw new Exception(String.Format("Failed to generate wcf contract code [Bad Result: {0}]", exitCode));
        }
        catch (Exception e)
        {
            if (e is TargetInvocationException)
                e = e.InnerException;

            info.E = e;
        }
4

3 回答 3

5

你已经知道 svcutil 支持这个选项(/reference 标志)。所以你只需要在反射器中打开 svcutil.exe 并执行与此方法相同的操作:Microsoft.Tools.ServiceModel.SvcUtil.ImportModule+InitializationHelper.InitReferencedContracts

internal static void InitReferencedContracts(Options options, WsdlImporter importer, ServiceContractGenerator contractGenerator)
{
    foreach (Type type in options.ReferencedTypes)
    {
        if (type.IsDefined(typeof(ServiceContractAttribute), false))
        {
            try
            {
                ContractDescription contract = ContractDescription.GetContract(type);
                XmlQualifiedName key = new XmlQualifiedName(contract.Name, contract.Namespace);
                importer.KnownContracts.Add(key, contract);
                contractGenerator.ReferencedTypes.Add(contract, type);
                continue;
            }
            catch (Exception exception)
            {
                if (Tool.IsFatal(exception))
                {
                    throw;
                }
                throw new ToolRuntimeException(SR.GetString("ErrUnableToLoadReferenceType", new object[] { type.AssemblyQualifiedName }), exception);
            }
        }
    }
}
于 2012-05-22T17:56:14.207 回答
1

如果您想要重用类型的程序集实际上被 web 服务的合同使用,那么像@peer 建议的那样添加它应该可以工作!所有 svcutil 应该需要的是 /reference 选项,它映射到他所说的内容。如果不是,则 svcutil.exe 认为程序集中的类型“A”和服务中的类型“A”不同。要么名称或名称空间存在细微差别,要么 - 在 XML 名称空间中(或架构实际上不同)。

请检查添加服务引用时忽略“重用现有类型” ——我的意思是,确保“现有程序集”中的“现有类型”确实映射到相同的模式领域。请注意,该合同属性必须在定义类型的程序集中!如果它是你的,只需添加并重新编译。

另外,您是否尝试过在控制台中手动运行 svcutil?您可能会在那里收到一些额外的错误/警告,也许他们会指出实际问题是什么。

于 2012-05-22T14:02:40.420 回答
1

这就是属性ServiceContractGenerator.ReferencedTypes的用途。只需为合约添加相应的映射类型即可。

于 2012-05-24T05:05:48.920 回答