蓝狐在DotNet中的思考 BlueFox Thinking in DotNet...
交流,分享,探讨DotNet技术...... Communion,Share, and Discuss about DotNet Technology.....

2009年11月27日

     通常要手动生成WebService代理类需要把一句生成语句,如 wsdl.exe /l:cs  /out:D:\Proxy_UpdateService.cs  http://localhost:1101/UpdateService.asmx?wsdl拷贝到Visual Studio 2005 命令提示窗口中去执行,这里再介绍另一个办法。

     首先打开Visual Studio 2005,选择菜单"工具"-"外部工具"打开外部工具对话框,如图,单击“添加”按钮添加新工具,然后在“标题”行中输入"WSDL生成代理类","命令"行中输入"C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\wsdl.exe"(wsdl.exe文件的路径),"参数"行中输入"/l:cs  /out:", 单击"初始目录"行右边的三角按钮选择"项目录",勾选"使用输出窗口"和"提示输入参数",然后确定保存。

     再打开菜单"工具"可以看到多了一个"WSDL生成代理类"菜单,这时先选定一个存放生成的代理类的文件夹(必须位于并且包含于当前解决方案中),然后单击"WSDL生成代理类"菜单,弹出如下对话框,然后你只需在"/l:cs  /out:"后面空一格(必须空一格)再粘贴WebService文件的http地址如http://localhost:4478/UpdateService.asmx?wsdl,单击"确定"看看发生了什么?是的,输出窗口会显示生成了一个类及其存放的位置,看看是不是你选定的文件夹,找到这个路径看看是不是有一个类,你会发现这个类跟上面使用命令行生成的类一模一样,个人觉得这样操作起来更简单一点。

 

 

posted @ 2009-11-27 18:15 蓝狐 阅读(47) | 评论(0) |  编辑
 
由WSDL文件生成WEB service server端C#程序
 
一般一个已经实现功能的WEB Server会发布自己的WSDL文件,供客户端生成代理类。

但有时是先有的server与client交互的接口定义(WSDL)文件,然后由server和client端分别写程序,一个提供web服务,一个使用web服务。

以下介绍如何由已有的WSDL文件在VS2005中生成server端代码。

1)使用VS2005提供的工具wsdl.exe由WSDL文件生成cs文件:

使用wsdl.exe的/serverInterface选项(或缩写的 /si)指定输入的wsdl文件(注意,如果要转换的wsdl文件中import了其他wsdl文件,则所有文件都应列出,包括使用到的xsd文件也应列出)。输出将是 一个代码文件(默认是C#的,如果需要别的语言,参考MSDN中wsdl.exe的使用说明),其中包含每个 wsdl 绑定的接口。

示例:假设ServerInterfaceSample.wsdl文件中import了importedSample.wsdl,并使用Service.xsd作为schema文件;

wsdl.exe /si ServerInterfaceSample.wsdl importedSample.wsdl Service.xsd

生成代码如下:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:2.0.50727.42
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Serialization;

//
// This source code was auto-generated by wsdl, Version=2.0.50727.42.
//


/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "2.0.50727.42")]
[System.Web.Services.WebServiceBindingAttribute(Name="org.csapi.cs.IpChargingManagerSOAPBinding", Namespace="http://www.csapi.org/cs/wsdl")]
public interface IOrgcsapicsIpChargingManagerSOAPBinding {
   
    /// <remarks/>
    [System.Web.Services.WebMethodAttribute()]
    [System.Web.Services.Protocols.SoapDocumentMethodAttribute("", RequestElementName="org.csapi.cs.IpChargingManager.createChargingSession", RequestNamespace="http://www.csapi.org/cs/schema", ResponseElementName="org.csapi.cs.IpChargingManager.createChargingSessionResult", ResponseNamespace="http://www.csapi.org/cs/schema", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
    [return: System.Xml.Serialization.XmlElementAttribute("return", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    orgcsapicsTpChargingSessionID createChargingSession([System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] EndpointReferenceType appChargingSession, [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] string sessionDescription, [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] orgcsapicsTpMerchantAccountID merchantAccount, [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] orgcsapiTpAddress user, [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] orgcsapicsTpCorrelationID correlationID);
   
    /// <remarks/>
    [System.Web.Services.WebMethodAttribute()]
    [System.Web.Services.Protocols.SoapDocumentMethodAttribute("", RequestElementName="org.csapi.cs.IpChargingManager.createSplitChargingSession", RequestNamespace="http://www.csapi.org/cs/schema", ResponseElementName="org.csapi.cs.IpChargingManager.createSplitChargingSessionResult", ResponseNamespace="http://www.csapi.org/cs/schema", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
    [return: System.Xml.Serialization.XmlElementAttribute("return", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    orgcsapicsTpChargingSessionID createSplitChargingSession([System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] EndpointReferenceType appChargingSession, [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] string sessionDescription, [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] orgcsapicsTpMerchantAccountID merchantAccount, [System.Xml.Serialization.XmlArrayAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] [System.Xml.Serialization.XmlArrayItemAttribute("item", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=false)] orgcsapiTpAddress[] users, [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] orgcsapicsTpCorrelationID correlationID);
   
    /// <remarks/>
    [System.Web.Services.WebMethodAttribute()]
    [System.Web.Services.Protocols.SoapDocumentMethodAttribute("", RequestElementName="org.csapi.IpService.setCallback", RequestNamespace="http://www.csapi.org/osa/schema", ResponseElementName="org.csapi.IpService.setCallbackResult", ResponseNamespace="http://www.csapi.org/osa/schema", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
    void setCallback([System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] EndpointReferenceType appInterface);
   
    /// <remarks/>
    [System.Web.Services.WebMethodAttribute()]
    [System.Web.Services.Protocols.SoapDocumentMethodAttribute("", RequestElementName="org.csapi.IpService.setCallbackWithSessionID", RequestNamespace="http://www.csapi.org/osa/schema", ResponseElementName="org.csapi.IpService.setCallbackWithSessionIDResult", ResponseNamespace="http://www.csapi.org/osa/schema", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
    void setCallbackWithSessionID([System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] EndpointReferenceType appInterface, [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] int sessionID);
}

代码中有一个接口类,并给出了接口函数声明.

此功能的优点是将实现代码和协定代码(如接口所定义的那样)分开。 如果更改 wsdl,然后重新生成接口,则不会丢失任何实现代码。 若要遵循建议的设计指南,则不应将任何代码放置在更改 wsdl 协定的实现中。 同样,不应使用影响运行时行为的代码来更改接口。 协定和实现的详细信息应该分开。

2) 在VS IDE中新建一个web service工程,加入新生成的**Interface.cs文件.
将自动生成的Service类改为从加入的Interface接口类派生,并实现所有的接口函数.(注意名空间一致)

[WebService(Namespace = "http://www.csapi.org/cs/wsdl")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : IOrgcsapicsIpChargingManagerSOAPBinding
{
    public orgcsapicsTpChargingSessionID createChargingSession(EndpointReferenceType appChargingSession, string sessionDescription, orgcsapicsTpMerchantAccountID merchantAccount, orgcsapiTpAddress user,orgcsapicsTpCorrelationID correlationID)
    {
        return new orgcsapicsTpChargingSessionID();
    }

    public orgcsapicsTpChargingSessionID createSplitChargingSession(EndpointReferenceType appChargingSession, string sessionDescription, orgcsapicsTpMerchantAccountID merchantAccount, orgcsapiTpAddress[] users, orgcsapicsTpCorrelationID correlationID)
    {
        return new orgcsapicsTpChargingSessionID();
    }

    public void setCallback(EndpointReferenceType appInterface)
    {

    }

    public void setCallbackWithSessionID(EndpointReferenceType appInterface, int sessionID)
    {

    }
   
}

大功告成,在实现函数中加入处理代码就可以了.
posted @ 2009-11-27 18:14 蓝狐 阅读(18) | 评论(0) |  编辑

2009年10月30日

     摘要: 个人觉得写的不错,但忘了是从哪里摘来的了。在以下的文章中,我将以“办公自动化”系统为例,探讨如何在有着1000万条数据的MS SQL SERVER数据库中实现快速的数据提取和数据分页。以下代码说明了我们实例中数据库的“红头文件”一表的部分数据结构:CREATETABLE[dbo].[TGongwen](--TGongwen是红头文件表名[Gid][i...  阅读全文
posted @ 2009-10-30 13:58 蓝狐 阅读(13) | 评论(0) |  编辑

2009年10月16日

Predicate 泛型委托

  表示定义一组条件并确定指定对象是否符合这些条件的方法。此委托由 ArrayList 类的几种方法使用,用于在集合中搜索元素。

看看下面它的定义:

    // Summary:
    
//     Represents the method that defines a set of criteria and determines whether
    
//     the specified object meets those criteria.
    
//
    
// Parameters:
    
//   obj:
    
//     The object to compare against the criteria defined within the method represented
    
//     by this delegate.
    
//
    
// Type parameters:
    
//   T:
    
//     The type of the object to compare.
    
//
    
// Returns:
    
//     true if obj meets the criteria defined within the method represented by this
    
//     delegate; otherwise, false.
    public delegate bool Predicate<T>(T obj);

类型参数介绍:

   T: 要比较的对象的类型。

   obj: 要按照由此委托表示的方法中定义的条件进行比较的对象。

   返回值:如果 obj 符合由此委托表示的方法中定义的条件,则为 true;否则为 false

 看下面代码:

    public class GenericDelegateDemo
    {
        List
<String> listString = new List<String>()
        {
            
"One","Two","Three","Four","Fice","Six","Seven","Eight","Nine","Ten"
        };

        String[] arrayString 
= new String[] 
        {
             
"One","Two","Three","Four","Fice","Six","Seven","Eight","Nine","Ten"
        };

        
public String[] GetFirstStringFromArray()
        {
            
return Array.FindAll(arrayString, (c) => { return c.Length <= 3; });
        }

        
public List<String> GetFirstStringFromList()
        {
            
return listString.FindAll((c) => { return c.Length <= 3; });
        }

        
public String[] GetFirstStringFromArray_1()
        {
            
return Array.FindAll(arrayString, GetString);
        }

        
public List<String> GetFirstStringFromList_1()
        {
            
return listString.FindAll(GetString);
        }

        
private bool GetString(String str)
        {
            
if (str.Length <= 3)
                
return true;
            
else
                
return false;
        }
    }

 

(1)首先,上面以 数组和泛型List 两个集合作为演示对象,并构建集合。

(2)接着,两者同时使用各自 所有的 FindALL方法,参见如下定义:

    Array : public T[] FindAll<T>(T[] array, Predicate<T> match);

    List:public List<T> FindAll(Predicate<T> match);

    注意的是,两处的FindAll 均采用了Predicate (泛型委托)作为参数的类型。

(3)接着,使用两者方式展现 对Predicate 的使用:

    第一种:  (c) => { return c.Length <= 3; };

    第二种: GetString(String str)。

这两者在语法上明显不同,但是实际是做相同的事情,第一种是使用Lambda表达式构建的语句,关于Lambda这里不做详述,请参见笔者C#3.0特性相关文章。

补充的是你也可以这样写,

  delegate(String c){return c.Length<=3;} 作为 Predicate定义的参数

完整代码:

XX.FindAll(delegate(String c) { return c.Length <= 3; }); 

这应该称为匿名代理了。

其他使用到Predicate 有

  Array.Find , Array.FindAll , Array.Exists , Array.FindLast , Array.FindIndex .....

  List<T>.Find , List<T>.FindAll , List<T>.Exists , List<T>.FindLast , List<T>.FindIndex .....

延伸:

  除了上面提到的外,你完全可以使用Predicate 定义新的方法,来加强自己代码。

 

public class GenericDelegateDemo
{
    List
<String> listString = new List<String>()
    {
        
"One","Two","Three","Four","Fice","Six","Seven","Eight","Nine","Ten"
    };


    
public String GetStringList(Predicate<String> p)
    {
        
foreach(string item in listString)
        {
            
if (p(item))
                
return item; 
        }
        
return null;
    }


    
public bool ExistString()
    {
        
string str = GetStringList((c) => { return c.Length <= 3 && c.Contains('S'); });
        
if (str == null)
            
return false;
        
else
            
return true;

    }
}

 

同样解决了上面的问题,这里罗嗦了只是为说明Predicate的用法而已。

对于Predicate的应用当然这不是什么新鲜事情,今天细细思味一番,觉得C# 真是一门优雅的语言。

以供初学者参考。

笔者本想对以下几种泛型委托一一做些介绍和总结的,在理解Predicate的过程中,发现只要理解了泛型、委托和匿名代理,

当然你晓得Lambda表达式更好,就完全可以在适当的时候灵活应用他们了。也就是说,只是定义不同的delegate而已,

一是 你可以自己定义这样的delegate,再行使用;

二是 你需要知道象Predicate、Func、Action这样的已有好的delegate是如何定义的。或者使用的时候适当查阅下MSDN即可。

如:

Func():封装一个不具有参数但却返回 TResult 参数指定的类型值的方法。

Func(T1, T2, TResult):封装一个具有两个参数并返回 TResult 参数指定的类型值的方法,没有T2就是封装一个具有参数并....。

Action() Action(T1) Action(T2) : 封装一个方法,该方法指定数量的参数(如()无参数,(T1)一个参数,以此类推)并且不返回值。这个和Func有相似处,但无返回值而已。

 提醒大家的注意的是:

  x=>x+x;

    与

  X=> {return x+x;} 是等价的。

posted @ 2009-10-16 16:59 蓝狐 阅读(54) | 评论(0) |  编辑

2009年8月4日

1.查找不重复记录
 例:ORGAN_ID列没有重复,Enterpriseid有重复记录,查找出Enterpriseid不重复的记录
 SELECT *
 FROM BPI_ORGAN
 WHERE (ORGAN_ID IN
          (SELECT MIN(ORGAN_ID)
         FROM BPI_ORGan
         GROUP BY Enterpriseid))
posted @ 2009-08-04 14:28 蓝狐 阅读(36) | 评论(0) |  编辑

2009年4月14日

在SharePoint 3.0 管理中心->操作->全局配置 "备用访问映射"->编辑公用 URL ->通过"备用访问映射集"下拉列表中的"修改备用访问映射集"确定要提供内网和外网访问的站点,并在下面的Intranet 和 Internet 项目中设置内外网访问地址或域名.

posted @ 2009-04-14 18:23 蓝狐 阅读(166) | 评论(0) |  编辑

2008年10月30日

     摘要: 跨服务器数据库操作SQL--增加服务器连接sp_addlinkedserver [192.168.10.209]--登录服务器EXEC sp_addlinkedsrvlogin '192.168.10.209', --被访问的服务器别名 'false', NULL, 'sa', --帐号 '123456' --密码--从本地服务器向远程服务器上的表插入数据insert into[192.168...  阅读全文
posted @ 2008-10-30 18:17 蓝狐 阅读(127) | 评论(0) |  编辑

2008年10月11日

     摘要: SQL2000和SQL2005的行转列处理方法2007-10-26 09:24 原表结构 SELECT [show_id] ,[year] ,[n1] ,[n2] ,[n3] ,[n4] ,[n5] FROM [datapass_db].[dbo].[test] 1:SQL2005中的列转行. SELECT [show_id], mon, SubTotal FROM [datapass_db].[...  阅读全文
posted @ 2008-10-11 14:35 蓝狐 阅读(129) | 评论(0) |  编辑

2008年10月4日

     摘要: 在一个产品介绍网站中查询产品时,由于产品的介绍性文字可能会很长,如果使用对产品介绍字段使用like进行模糊查询,性能肯定会是问题。那么如何解决这个问题呢?第一个想法就是使用全文索引。那么全文索引是什么、应该如何应用、在应用的过程中又应该注意哪些事情呢?1、是什么全文索引 [摘录自SQL Server2000联机从书] 全文索引为在字符串数据中进行复杂的词搜索提供有效支持。全文索引存储关于重要词和...  阅读全文
posted @ 2008-10-04 15:33 蓝狐 阅读(416) | 评论(0) |  编辑
 
     摘要: 全文检索是对大数据文本进行索引,在建立的索引中对要查找的单词进行进行搜索,定位哪些文本数据包括要搜索的单词。因此,全文检索的全部工作就是建立索引和在索引中搜索定位,所有的工作都是围绕这两个来进行的。下面就逐个介绍。   建立全文索引中有两项非常重要,一个是如何对文本进行分词,一是建立索引的数据结构。分词的方法基本上是二元分词法、最大匹配法和统计方法。索引的数据结构基本上采用倒排索引的结构。  分词...  阅读全文
posted @ 2008-10-04 14:55 蓝狐 阅读(529) | 评论(0) |  编辑