您的位置:程序门 -> .net技术 -> web services



关于异步webservice中使用httpcontext的问题


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


关于异步webservice中使用httpcontext的问题[已结贴,结贴人:crazyboymowei]
发表于:2007-12-20 17:36:01 楼主
请问怎样在异步的webservice中使用httpcontext获取客户端的ip,我搞了半天老是报两个错;
一个是如果直接用httpcontext.current.request.userhostaddress.tostring()会报未将对象引用到对象的实例的错,
一个是把httpcontext当参数传递会报httpcontext不能序列化,没用无参数的构造函数


我的代码如下:

public   class   service   :   system.web.services.webservice,system.web.ihttpasynchandler
{
        //定义一个委托
        public   delegate   result   delegateconfig(string   modulename,   string   servername,   string   configkey);
        public   service   ()   {
                //initializecomponent();  
        }
        [webmethod]
        public   iasyncresult   begingetserverconfig(string   modulename,   string   servername,   string   configkey,   asynccallback   callback,object   state)  
        {
                delegateconfig   dconfig   =   new   delegateconfig(getconfig);
                asyncstate   state   =   new   asyncstate(state,   dconfig);

                return   dconfig.begininvoke(modulename,   servername,   configkey,   callback,   state);
        }
        [webmethod]
        public   result   endgetserverconfig(iasyncresult   result)
        {
                asyncstate   state   =   (asyncstate)result.asyncstate;

                return   state.delegateconfig.endinvoke(result);
        }
        [webmethod]
        [xmlinclude(typeof(result))]
        public   result   getconfig(string   modulename,   string   servername,   string   configkey)
        {
                //这是一个同步方法
                return   result;
        }

        public   class   asyncstate
        {
                public   object   state;
                public   delegateconfig   delegateconfig;

                public   asyncstate(object   state,   delegateconfig   delegateconfig)
                {
                        this.state   =   state;
                        this.delegateconfig   =   delegateconfig;
                }
        }
}
[serializable]
public   class   result
{
        private   string   _value;
        public   string   value
        {
                get   {   return   _value;   }
                set   {   _value   =   value;   }
        }
        private   string   _newurl;
        public   string   newurl
        {
                get   {   return   _newurl;   }
                set   {   _newurl   =   value;   }
        }
        private   string   _timeout;
        public   string   timeout
        {
                get   {   return   _timeout;   }
                set   {   _timeout   =   value;   }
        }
        private   string   _error;
        public   string   error
        {
                get{return   _error;}
                set{_error=value;}
        }
}

分不够再加..希望大家帮帮小弟,在线等
发表于:2007-12-20 17:42:491楼 得分:0
不懂,关注
发表于:2007-12-21 09:30:312楼 得分:0
能力有限阿,只能帮顶了
发表于:2007-12-21 09:39:093楼 得分:0
.....难道就没人知道吗?
发表于:2007-12-21 09:42:234楼 得分:0
帮你顶一下了
.....
发表于:2007-12-21 10:23:475楼 得分:150
不要用httpcontext   因为异步webservice中的httpcontext是不会自己传过来的,所以你用httpcontext.current.request.userhostaddress获取的值是null,然后你再tostring()肯定会报错,但是httpcontext.current.request.userhostaddress   as     string   是不会报错的,不过结果也是为null.

lz可以试下   context.request.servervariables["remote_addr"],在webservice中也可以获取到客户端ip
发表于:2007-12-21 11:07:146楼 得分:0
mark
发表于:2007-12-25 15:12:177楼 得分:0
谢谢   whotnet.问题解决了


快速检索

最新资讯
热门点击