Reporting Services动态引用WebServices发布报表
WebClient web = new WebClient();
web.Credentials = System.Net.CredentialCache.DefaultCredentials;
Stream stream = web.OpenRead("WebServices地址");
// 2. 创建和格式化 WSDL 文档。
ServiceDescription description = ServiceDescription.Read(stream);
// 3. 创建客户端代理代理类。
ServiceDescriptionImporter importer = new ServiceDescriptionImporter();
importer.ProtocolName = "Soap"; // 指定访问协议。
importer.Style = ServiceDescriptionImportStyle.Client; // 生成客户端代理。
importer.CodeGenerationOptions = CodeGenerationOptions.GenerateProperties | CodeGenerationOptions.GenerateNewAsync;
importer.AddServiceDescription(description, null, null); // 添加 WSDL 文档。
// 4. 使用 CodeDom 编译客户端代理类。
CodeNamespace nmspace = new CodeNamespace(); // 为代理类添加命名空间,缺省为全局空间。
CodeCompileUnit unit = new CodeCompileUnit();
unit.Namespaces.Add(nmspace);
ServiceDescriptionImportWarnings warning = importer.Import(nmspace, unit);
CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");
CompilerParameters parameter = new CompilerParameters();
parameter.GenerateExecutable = false;
parameter.GenerateInMemory = true;
parameter.ReferencedAssemblies.Add("System.dll");
parameter.ReferencedAssemblies.Add("System.XML.dll");
parameter.ReferencedAssemblies.Add("System.Web.Services.dll");
parameter.ReferencedAssemblies.Add("System.Data.dll");
CompilerResults result = provider.CompileAssemblyFromDom(parameter, unit);
// 5. 使用 Reflection 调用 WebService。
if (!result.Errors.HasErrors)
{
Assembly asm = result.CompiledAssembly;
Type TRptService = asm.GetType("ReportingService"); // 如果在前面为代理类添加了命名空间,此处需要将命名空间添加到类型前面。
object objRptService = Activator.CreateInstance(TRptService); // 获取实例
//设置Credentials属性值(xml web service客户端的验证凭据)
PropertyInfo pRptServiceCredentials = TRptService.GetProperty("Credentials");
pRptServiceCredentials.SetValue(objRptService, System.Net.CredentialCache.DefaultCredentials, null);
//获取CredentialRetrievalEnum的值,用来存储数据源的密码是否保存
Type TCredentialRetEnum = asm.GetType("CredentialRetrievalEnum");
//对数据源的定义并且发布到服务器.
Type TDSDefinition = asm.GetType("DataSourceDefinition");
object objDSDefinition = Activator.CreateInstance(TDSDefinition);
PropertyInfo pCredentialRetrieval = TDSDefinition.GetProperty("CredentialRetrieval");
pCredentialRetrieval.SetValue(objDSDefinition, Enum.Parse(TCredentialRetEnum, "Store", false), null);
SetProperty(objDSDefinition, sConnect, "数据库连接信息", TDSDefinition);
SetProperty(objDSDefinition, true, "Enabled", TDSDefinition);
SetProperty(objDSDefinition, true, "EnabledSpecified", TDSDefinition);
SetProperty(objDSDefinition, "SQL", "Extension", TDSDefinition);
SetProperty(objDSDefinition, true, "ImpersonateUserSpecified", TDSDefinition);
SetProperty(objDSDefinition, "数据库登陆名", "UserName", TDSDefinition);
SetProperty(objDSDefinition, "数据库登陆密码", "Password", TDSDefinition);
SetProperty(objDSDefinition, false, "WindowsCredentials", TDSDefinition);
MethodInfo MCreateDS = TRptService.GetMethod("CreateDataSource");
object[] Paramers = new object[] { "报表数据库连接信息", "报表存放目录", true, objDSDefinition, null };
MCreateDS.Invoke(objRptService, Paramers);
//发布报表文件.
MethodInfo MCreateRPT = TRptService.GetMethod("CreateReport");
object[] ParamersRPT = new object[] { "报表文件名字", "报表存放目录", true, "报表文件", null };
MCreateRPT.Invoke(objRptService, ParamersRPT);
}
private void SetProperty(object obj, object objValue, string sPropertyName, Type t)
{
PropertyInfo p = t.GetProperty(sPropertyName);
p.SetValue(obj, objValue, null);
}
web.Credentials = System.Net.CredentialCache.DefaultCredentials;
Stream stream = web.OpenRead("WebServices地址");
// 2. 创建和格式化 WSDL 文档。
ServiceDescription description = ServiceDescription.Read(stream);
// 3. 创建客户端代理代理类。
ServiceDescriptionImporter importer = new ServiceDescriptionImporter();
importer.ProtocolName = "Soap"; // 指定访问协议。
importer.Style = ServiceDescriptionImportStyle.Client; // 生成客户端代理。
importer.CodeGenerationOptions = CodeGenerationOptions.GenerateProperties | CodeGenerationOptions.GenerateNewAsync;
importer.AddServiceDescription(description, null, null); // 添加 WSDL 文档。
// 4. 使用 CodeDom 编译客户端代理类。
CodeNamespace nmspace = new CodeNamespace(); // 为代理类添加命名空间,缺省为全局空间。
CodeCompileUnit unit = new CodeCompileUnit();
unit.Namespaces.Add(nmspace);
ServiceDescriptionImportWarnings warning = importer.Import(nmspace, unit);
CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");
CompilerParameters parameter = new CompilerParameters();
parameter.GenerateExecutable = false;
parameter.GenerateInMemory = true;
parameter.ReferencedAssemblies.Add("System.dll");
parameter.ReferencedAssemblies.Add("System.XML.dll");
parameter.ReferencedAssemblies.Add("System.Web.Services.dll");
parameter.ReferencedAssemblies.Add("System.Data.dll");
CompilerResults result = provider.CompileAssemblyFromDom(parameter, unit);
// 5. 使用 Reflection 调用 WebService。
if (!result.Errors.HasErrors)
{
Assembly asm = result.CompiledAssembly;
Type TRptService = asm.GetType("ReportingService"); // 如果在前面为代理类添加了命名空间,此处需要将命名空间添加到类型前面。
object objRptService = Activator.CreateInstance(TRptService); // 获取实例
//设置Credentials属性值(xml web service客户端的验证凭据)
PropertyInfo pRptServiceCredentials = TRptService.GetProperty("Credentials");
pRptServiceCredentials.SetValue(objRptService, System.Net.CredentialCache.DefaultCredentials, null);
//获取CredentialRetrievalEnum的值,用来存储数据源的密码是否保存
Type TCredentialRetEnum = asm.GetType("CredentialRetrievalEnum");
//对数据源的定义并且发布到服务器.
Type TDSDefinition = asm.GetType("DataSourceDefinition");
object objDSDefinition = Activator.CreateInstance(TDSDefinition);
PropertyInfo pCredentialRetrieval = TDSDefinition.GetProperty("CredentialRetrieval");
pCredentialRetrieval.SetValue(objDSDefinition, Enum.Parse(TCredentialRetEnum, "Store", false), null);
SetProperty(objDSDefinition, sConnect, "数据库连接信息", TDSDefinition);
SetProperty(objDSDefinition, true, "Enabled", TDSDefinition);
SetProperty(objDSDefinition, true, "EnabledSpecified", TDSDefinition);
SetProperty(objDSDefinition, "SQL", "Extension", TDSDefinition);
SetProperty(objDSDefinition, true, "ImpersonateUserSpecified", TDSDefinition);
SetProperty(objDSDefinition, "数据库登陆名", "UserName", TDSDefinition);
SetProperty(objDSDefinition, "数据库登陆密码", "Password", TDSDefinition);
SetProperty(objDSDefinition, false, "WindowsCredentials", TDSDefinition);
MethodInfo MCreateDS = TRptService.GetMethod("CreateDataSource");
object[] Paramers = new object[] { "报表数据库连接信息", "报表存放目录", true, objDSDefinition, null };
MCreateDS.Invoke(objRptService, Paramers);
//发布报表文件.
MethodInfo MCreateRPT = TRptService.GetMethod("CreateReport");
object[] ParamersRPT = new object[] { "报表文件名字", "报表存放目录", true, "报表文件", null };
MCreateRPT.Invoke(objRptService, ParamersRPT);
}
private void SetProperty(object obj, object objValue, string sPropertyName, Type t)
{
PropertyInfo p = t.GetProperty(sPropertyName);
p.SetValue(obj, objValue, null);
}