12,162
社区成员
发帖
与我相关
我的任务
分享
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
public Service () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
public MySoapHeader mySoapHead;
[WebMethod(EnableSession = true)]
[SoapHeader("mySoapHead", Direction = SoapHeaderDirection.InOut)]
public bool CheckKey()
{
if (mySoapHead == null)
{
return false;
}
else
{
string key = System.Configuration.ConfigurationManager.AppSettings["SoapHeaderKey"].ToString().Trim();
key = FormsAuthentication.HashPasswordForStoringInConfigFile(key, "MD5");
if (key == mySoapHead.Key)
{
Session["Key"] = key;
return true;
}
else
{
return false;
}
}
}
[WebMethod(EnableSession = true)]
public string HelloWorld()
{
if (Session["Key"] != null)
{
return "Hello World";
}
else
{
return "Error";
}
}
}
public class MySoapHeader : SoapHeader
{
private string key;
public string Key
{
get { return key; }
set { key = value; }
}
}
// 1. 使用 WebClient 下载 WSDL 信息。
WebClient web = new WebClient();
Stream stream = web.OpenRead("http://localhost/MappingService/Service.asmx");
// 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.OutputAssembly = "E:\\MappingService.dll"; // 可以指定你所需的任何文件名。
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);