问题:

当我尝试启动在C#中创建的Windows服务时出现此错误:

替代文字

到目前为止,我的代码:

private ServiceHost host = null;

public RightAccessHost()
{
    InitializeComponent();
}

protected override void OnStart(string[] args)
{
    host = new ServiceHost(typeof(RightAccessWcf));
    host.Open();
}

protected override void OnStop()
{
    if (host != null)
        host.Close();
    host = null;
}

更新#1

我通过向帐户NETWORK SERVICE授予权限解决了上述问题,但现在又遇到了另一个问题:

替代文字

更新#2

服务无法启动。 System.InvalidOperationException:服务’RightAccessManagementWcf.RightAccessWcf’具有零个应用程序端点(非基础结构)。 这可能是因为没有为您的应用程序找到配置文件,或者是因为在配置文件中找不到与服务名称匹配的服务元素,或者因为在服务元素中未定义端点。 在System.ServiceModel.Description.DispatcherBuilder.EnsureThereAreNonMexEndpoints(ServiceDescription描述)在System.ServiceModel.Description.DispatcherBuilder.InitializeServiceHost(ServiceDescription描述,ServiceHostBase serviceHost)在System.ServiceModel.ServiceHostBase.InitializeRuntime()在System.ServiceModel.ServiceHostBase.OnOpen()在System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan超时)在C:\\ Users中的RightAccessHosting.RightAccessHost.OnStart(String [] args)处。

I’m getting this error when I try to start a windows service I’ve created in C#:My Code so far:Update #1I solved the issue above by granting permissions to the account NETWORK SERVICE but now I have an another problem:Update #2Service cannot be started.System.InvalidOperationException: Service ‘RightAccessManagementWcf.RightAccessWcf’ has zero application (non-infrastructure) endpoints.This might be because no configuration file was found for your application, or because no service element matching the service name could be found in the configuration file, or because no endpoints were defined in the service element.at System.ServiceModel.Description.DispatcherBuilder.EnsureThereAreNonMexEndpoints(ServiceDescription description) at System.ServiceModel.Description.DispatcherBuilder.InitializeServiceHost(ServiceDescription description, ServiceHostBase serviceHost) at System.ServiceModel.ServiceHostBase.InitializeRuntime() at System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout) at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) at RightAccessHosting.RightAccessHost.OnStart(String[] args) in C:\\Users….

问题:

我是C#的新手,不明白为什么以下代码不起作用。

public static Nullable<T> CoalesceMax<T>(Nullable<T> a, Nullable<T> b) where T : IComparable
{
    if (a.HasValue && b.HasValue)
        return a.Value.CompareTo(b.Value) < 0 ? b : a;
    else if (a.HasValue)
        return a;
    else
        return b;
}

// Sample usage:
public DateTime? CalculateDate(DataRow row)
{
    DateTime? result = null;
    if (!(row["EXPIRATION_DATE"] is DBNull))
        result = DateTime.Parse((string)row["EXPIRATION_DATE"]);
    if (!(row["SHIPPING_DATE"] is DBNull))
        result = CoalesceMax(
            result
            DateTime.Parse((string)row["SHIPPING_DATE"]).AddYears(1));
    // etc.
    return result;
}

它在编译期间出现以下错误:


类型’T’必须是非可空值类型,以便在泛型类型或方法’System.Nullable <T>’中将其用作参数’T’

I’m new to C# and don’t understand why the following code doesn’t work.It gives the following error during compilation:The type ‘T’ must be a non-nullable value type in order to use it as parameter ‘T’ in the generic type or method ‘System.Nullable<T>’

问题:

任何想法如何解决这一问题?

UserService.UserServiceClient userServiceClient = new UserServiceClient();
            userServiceClient.GetUsersCompleted += new EventHandler<GetUsersCompletedEventArgs>(userServiceClient_GetUsersCompleted);
            userServiceClient.GetUsersAsync(searchString);

<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="BasicHttpBinding_UserService" 
                     maxBufferSize="2147483647" 
                     maxReceivedMessageSize="2147483647">
                <security mode="None" />
            </binding>
        </basicHttpBinding>
    </bindings>
    <client>
        <endpoint address="http://localhost:52185/UserService.svc" 
                  binding="basicHttpBinding" 
                  bindingConfiguration="BasicHttpBinding_UserService" 
                  contract="UserService.UserService"
                  name="BasicHttpBinding_UserService" />
    </client>
    <behaviors>
        <serviceBehaviors>
            <behavior name="Shell.Silverlight.Web.Service3Behavior">
                <serviceMetadata httpGetEnabled="true" />
                <serviceDebug includeExceptionDetailInFaults="false" />
            </behavior>
        </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
    <services>
        <service behaviorConfiguration="Shell.Silverlight.Web.Service3Behavior" 
                 name="Shell.Silverlight.Web.Service3">
            <endpoint address="" 
                      binding="basicHttpBinding" 
                      contract="Shell.Silverlight.Web.Service3" />
            <endpoint address="mex" 
                      binding="mexHttpBinding" 
                      contract="IMetadataExchange" />
        </service>
    </services>
</system.serviceModel>

无法在ServiceModel客户端配置部分中找到引用合同“UserService.UserService”的默认端点元素。 这可能是因为没有为您的应用程序找到配置文件,或者因为在客户端元素中找不到与此合同匹配的端点元素。

解决!

我没有提到这是一个Silverlight应用程序。 我在DLL中有wcf引用,它有自己的“ServiceReferences.ClientConfig”文件。 我将DLL的ServiceReferences.ClientConfig的内容移动到主Silverlight项目并且它工作了。

Any ideas how to fix this?.Could not find default endpoint element that references contract ‘UserService.UserService’ in the ServiceModel client configuration section.This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element.Resolved!I didn’t mention that this was a Silverlight application.I had the wcf reference in a DLL which had it’s own “ServiceReferences.ClientConfig” file.I moved the contents of the DLL’s ServiceReferences.ClientConfig to the main silverlight project and it worked.

问题:

通常,我收到此错误:(本地计算机上的“服务名称”服务启动然后停止。如果我的代码出现问题,如某些服务未被其他服务或程序使用,则会自动停止驱动路径等.Windows服务无法启动。

如果达到大小限制,我有一个备份文件夹/文件的Windows服务到一个位置。 详细信息都是由Windows服务在启动时读取的XML配置提供的。 我有一个单独的窗体,有一个按钮,完全符合我的Windows服务的启动。 在将其放入Windows服务之前,我使用我的Windows窗体来调试代码。

当我启动我的Windows窗体。 它做了它想做的事情。 当我把我的代码放在Windows服务OnStart()方法时出现错误。

这是我的代码:

protected override void OnStart(string[] args)
{

    private static string backupConfig = @"D:\LogBackupConfig\backupconfig.xml";
    private static string serviceStat = @"D:\LogBackupConfig\Status.txt";
    private static string fileFolderStat = @"D:\LogBackupConfig\FileFolderStat.txt";

    protected override void OnStart(string[] args)
    {
        if (File.Exists(backupConfig))
        {
            FileSystemWatcher watcher = new FileSystemWatcher();
            XmlTextReader reader = new XmlTextReader(backupConfig);

            XmlNodeType type;
            List<string> listFile = new List<string>();
            string fileWatch = "";

            //this loop is for reading XML elements and assigning to variables
            while (reader.Read())
            {
                type = reader.NodeType;
                if (type == XmlNodeType.Element)
                {
                    if (reader.Name == "File")
                    {
                        reader.Read();
                        fileWatch = reader.Value;
                    }
                    else if (reader.Name == "Folder")
                    {
                        reader.Read();
                        fileWatch = reader.Value;
                    }
                }
            }
            reader.Close();

            watcher.Path = fileWatch;
            watcher.Filter = "*.*";

            //this loop reads whether the service will watch a file/folder
            XmlTextReader reader1 = new XmlTextReader(backupConfig);
            while (reader1.Read())
            {
                type = reader1.NodeType;
                if (type == XmlNodeType.Element)
                {
                    if (reader1.Name == "File")
                    {
                        watcher.IncludeSubdirectories = false;
                        watcher.Changed += new FileSystemEventHandler(OnChangedFile);
                    }
                    else if (reader1.Name == "Folder")
                    {
                        watcher.IncludeSubdirectories = true;
                        watcher.Changed += new FileSystemEventHandler(OnChangedFolder);
                    }
                }
            }
            reader1.Close();

            watcher.EnableRaisingEvents = true;

        }
        else
        {
            StreamWriter sw = new StreamWriter(serviceStat, true);
            sw.WriteLine("File not found. Please start the Log Backup UI first.");
            sw.Close();
        }
    }

我不知道是什么让Windows服务无法启动,Windows窗体模拟器工作正常。 什么似乎是问题?

更新:经过多次试验,我注意到只使用文件夹目录(w / out文件),Windows服务不起作用。 当我用特定文件(包括其目录)替换fileWatch变量时,Windows服务启动了。 当我将其更改回文件夹位置时,它无法正常工作。 我认为文件夹位置在filewatcher中不起作用。

当我尝试创建一个监视文件夹位置的新Windows服务时,它工作..但是,当我在原始的Windows服务中尝试相同的位置时,它不起作用! 我很精神!#ed ed! 似乎每次我放置一个新的代码/函数时我都必须创建一个新的Windows服务并构建安装程序。这样我就可以跟踪我收到错误的位置。

Usually, I get this error: (The “service name” service on Local Computer started and then stopped. Some services stop automatically if they are not in use by other service or programs) when there’s something wrong with my code, like non-existing drive paths, etc. The windows service will not start.I have a windows service that backs up folder/files, to a location if it reached the size limit.Details are all provide by an XML Configuration that the windows service reads on start.I have a separate windows forms that has a button that does exactly what my windows service’s onstart is doing.I use my windows forms for debugging the code before I put it in my windows service.When I start my windows forms.It does what it suppose to do.When I put my code in the windows service OnStart() method the error showed up.Here’s my code:I don’t know what keeps the windows service not starting, the windows form simulator worked fine.What seems to be the problem?UPDATE: After many trials I’ve noticed that using only a folder directory (w/out file), the windows service doesn’t work.When I replaced the fileWatch variable with a specific file (including its directory), the windows service started.When I changed it back to a folder location, it didn’t work.What I think is that folder locations doesn’t work in a filewatcher.When I tried creating a new windows service that watches a folder location, it worked.. However, when I tried the same location in my original windows service, it didn’t work!I was mindf$#*ed!It seems that I have to create a new windows service and build the installer everytime I place a new code/function.. This way I can keep track where I get an error.

问题:

我正在开发带有WebBrowser控件的Windows应用程序,该控件可导航到共享点站点。
我的问题是我遇到了JavaScript错误。

如何禁用JavaScript错误? 我不希望他们弹出。

I am developing a windows application with a WebBrowser control that navigates to a sharepoint site.My problem is that i am getting JavaScript error.How can i disable the JavaScript error?I don’t want them to pop up.