2008年3月小记(设置Windows服务的依赖关系,配置MSMQ的访问权限,WCF的安全配置,删除重建网站,HttpWebRequest,一年中的周,在WCF中使用HttpContext,String.Join)

1、如何设置Windows服务的依赖关系?
因为新开发的内容服务的WCF通信使用的是MSMQ接收用户内容的异步可离线信息,内容服务本身是一个windows服务,而这个服务必须使用MSMQ,所以就要求在机器启动时必须先启动MSMQ服务,然后才可以再启动内容服务,这就需要在内容服务中设定服务的依赖关系,此服务必须要在MSMQ服务启动后再启动。
修改ProjectInstaller.cs中的serviceInstaller1的属性ServicesDependedOn,此属性接收一个数组设置,一行代表一个依赖的服务,因为这里只依赖于MSMQ,所以填写MSMQ的服务名"MSMQ"就行了,重新编译安装内容服务后就可以了,此服务启动时,如果MSMQ没有启动,则会自动先启动MSMQ服务。

2、配置MSMQ的访问权限。
通过MessageQueue.Create创建的队列默认情况下不可以在控制台被Administrator控制或查看,这是因为没有配置权限的原因。可以在Create之后配置权限。
            if (!MessageQueue.Exists(@".\private$\ContentReceiverQueue"))
            
{
                MessageQueue messageQueue 
= MessageQueue.Create(@".\private$\ContentReceiverQueue"true);
                messageQueue.SetPermissions(
"Everyone", MessageQueueAccessRights.FullControl);
            }
通过为Everyone配置完全控制的权限,则可以防止这种情况的发生。当然如果需要精确的权限控制还需要你仔细配置才是。

3、注意WCF的安全配置。
选择不同的WCF通信协议可能会有不同的默认安全身份验证,如tcp的话就是winodws身份验证,如果没有相关设置在进行跨机器访问时可能就需要用资源管理器登录一下该服务器端,并且选择保存密码,当然如果在客户端传递了验证信息除外。如果在局域网上访问也可以彻底关闭该安全验证如:
<binding name="SampleContentBinding">
    
<security mode="None" />
</binding>

4、解决问题的一个可能方法:删除重建网站
网站中有一个虚拟目录service,其中提供了一个wcf服务*.svc 本地一切正常,当布署到测试服务器总是报"重新添加url地址"的错误,但是其它也是放在虚拟目录中的wcf并没有这样的错误,测了老半天发现还是网站本身的个案,所以重建了网站和虚拟目录,结果就好了。有时候这是一种最为便捷的排除方法。

5、HttpWebRequest请求一个页面的内容。
        string url = "xxxxxxxxxxxxx";

            HttpWebRequest request 
= (HttpWebRequest)HttpWebRequest.Create(url);
            request.Timeout 
= 5000;

            
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            
{
                
if (response.StatusCode == HttpStatusCode.OK)
                
{
                    
using (Stream stream = response.GetResponseStream())
                    
{
                        
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
                        
{
                            content 
= reader.ReadToEnd();
                        }

                    }

                }

            }

6、计算一年中的周。
        // 求某年有多少周
        public static int GetYearWeekCount(int year)
        
{
            
int count = 53;
            
if (DateTime.IsLeapYear(year) && (new DateTime(year, 11).DayOfWeek == DayOfWeek.Saturday))
            
{
                count 
= 54;
            }

            
return count;
        }


        
// 求当前日期是一年的中第几周
        public static int WeekOfYear(DateTime day)
        
{
            
return CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(day, CalendarWeekRule.FirstDay, DayOfWeek.Sunday);
        }

7、如何在在WCF中使用HttpContext
默认情况下,通过WCF调用无法像ASMX或者ASPX一样可以直接使用HttpContext对象,当然也不可以使用其中的上下文缓存了。为了和现在系统蒹容可以使用WCF ASP.NET 兼容模式选项。在这种模式下,WCF 服务以与 ASMX 服务类似的方式参与 ASP.NET HTTP 管线。ASP.NET 功能(例如,文件授权、URL 授权和 HTTP 会话状态)适用于在此模式下运行的 WCF 服务。步聚如下:
A、程序员必须将 AspNetCompatibilityRequirementsAttribute 属性添加到服务类型中,并指定是允许还是需要 ASP.NET 兼容模式。
    [ServiceBehavior]
    [AspNetCompatibilityRequirements(RequirementsMode 
= AspNetCompatibilityRequirementsMode.Required)]
    
public class SampleContent : ISampleContent
B、必须将应用程序配置为使用 ASP.NET 兼容模式。
    <system.serviceModel>
        
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"></serviceHostingEnvironment>
    
</system.serviceModel>

注意为了使WCF保持一致风格,可以使用InstanceContext对像。
        OperationContext operationContext = OperationContext.Current;
            InstanceContext instanceContext 
= operationContext.InstanceContext;
另外,WCF 应用程序也可以配置为使用 .asmx(而非 .svc)作为服务文件的扩展名。
<system.web>
     
<compilation>
      
<compilation debug="true">
      
<buildProviders>
       
<remove extension=".asmx"/>
       
<add extension=".asmx" 
        type
="System.ServiceModel.ServiceBuildProvider, 
        Systemm.ServiceModel, 
        Version=3.0.0.0, 
        Culture=neutral, 
        PublicKeyToken=b77a5c561934e089"
 />
      
</buildProviders>
      
</compilation>
     
</compilation>
</system.web>
此选项可以使您在将服务修改为使用 WCF 时,免于修改配置为使用 .asmx 服务文件 URL 的客户端。
Providing custom context to your WCF service instance
从开发的角度比较 ASP.NET Web 服务与 WCF
AspNetCompatibilityRequirementsAttribute

8、如何编写并发测试代码?

9、串联字符串
String.Join(",", new string[]{"a","b","c"})

 

 

posted @ 2008-03-07 10:16  网际飞狐  阅读(1777)  评论(1编辑  收藏  举报