GVKun编程网logo

c# – 在System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo)中拒绝访问

13

此处将为大家介绍关于c#–在System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfostartInfo)中拒绝访问的详细内容

此处将为大家介绍关于c# – 在System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo)中拒绝访问的详细内容,此外,我们还将为您介绍关于.net – ProcessStartInfo的UseShellExecute和CreateNoWindow有什么区别?、asp.net – System.Diagnostics.Process.Start不能从IIS工作、Bean后置处理器 - InstantiationAwareBeanPostProcessor#applyBeanPostProcessorsBeforeInstantiation、Bean后置处理器 - InstantiationAwareBeanPostProcessor#postProcessProperties的有用信息。

本文目录一览:

c# – 在System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo)中拒绝访问

c# – 在System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo)中拒绝访问

我试图上传文件并将其转换为另一种格式,然后将其保存在我的Web服务器上,但是我收到以下错误:
System.ComponentModel.Win32Exception(0x80004005):在System.Diagnostics.Process.Start()的System.Diagnostics.Process.StartWithCreateProcess(processstartinfo startInfo)中拒绝访问

当我尝试在本地计算机的Web服务器(Windows 7)上执行此操作时没有问题,但是在将我的网站部署到具有Windows Server 2008 R2的Web托管提供商后,我收到此错误.

我正在使用ASP.NET c#.我猜这是一个权限问题,但我不知道如何提升任何权限.请帮忙!

解决方法

我怀疑你可以授予标准ASPNET用户执行程序的权限(这也是不好的做法).更好的选择(不启动进程)将是将应用程序池的用户更改为有权执行该进程的服务器上的本地用户(最好是非管理员帐户).请注意,您启动的进程不能包含任何UI组件,因为您不会看到它正在运行.

.net – ProcessStartInfo的UseShellExecute和CreateNoWindow有什么区别?

.net – ProcessStartInfo的UseShellExecute和CreateNoWindow有什么区别?

如果我要编写一个控制台应用程序来启动另一个控制台应用程序作为新进程,并使用默认的“Use ShellExecute”和“CreateNowindow”值,结果是控制台应用程序窗口旁边会出现一个新的命令行窗口:

UseShellExecute=true and CreateNowindow=false

现在,如果我要使用以下内容,则不会创建新窗口,但启动过程的输出将出现在启动它的应用程序的窗口中:

UseShellExecute=false and CreateNowindow=false

如果我要使用以下内容,既不会创建新窗口,也不会在启动它的应用程序窗口中显示已启动进程的输出:

UseShellExecute=false and CreateNowindow=true

但是当UseShellExecute = true时,似乎“CreateNowindow”没有效果,那么当UseShellExecute = true时,“CreateNowindow”会扮演什么角色呢?仅当作为新进程启动的应用程序是Forms应用程序时,它是否适用?

解决方法

它没有任何作用.规则是CreateNowindow仅在以下情况下具有效果:

>您使用UseShellExecute = false,以便使用CreateProcess()winapi来启动该程序
>您启动的程序是控制台模式应用程序.

如果应用程序是创建自己的窗口的本机Windows GUI应用程序,那么您可以要求它不使用WindowStyle = ProcessWindowStyle.Hidden创建可见窗口.但是有很多程序忽略了这个请求.他们应该只通过任务管理器来阻止它.下一个合理的选择是ProcessWindowStyle.Minimized

asp.net – System.Diagnostics.Process.Start不能从IIS工作

asp.net – System.Diagnostics.Process.Start不能从IIS工作

当我从我的控制台应用程序运行System.Diagnostics.Process.Start时,它的工作原理是相同的代码,当我从IIS中托管的Web服务运行不起作用.

有什么事情要做ASP.Net的权限?如果是,我如何配置它从我的C#代码.

解决方法

ASP.NET网页和服务器控件代码在Web服务器上的ASP.NET工作进程的上下文中执行.如果在ASP.NET网页或服务器控件中使用Start方法,则新进程将在受限权限的Web服务器上执行.该进程不会在与客户端浏览器相同的上下文中启动,并且无法访问用户桌面.
http://msdn.microsoft.com/en-us/library/0w4h05yb.aspx

– 为ASP.NET工作进程帐户授予权限

与桌面交互或允许ASP.NET工作进程在SYstem帐户中运行.

>了解如何允许工作进程在SYstem帐户中运行
并了解ASPNET帐户的默认权限,请查看本文
INFO:ASP.NET中的进程和请求身份:http://support.microsoft.com/default.aspx?scid=kb;en-us;317012

– 启用IIS管理服务与桌面交互

要进行配置,请按照以下步骤操作.

> a.打开控制面板并按照下列步骤操作:对于Windows NT:单击服务.对于Windows 2000,Windows XP和.NET Server:单击管理工具,然后单击服务.> b.双击IIS管理服务.> c.在“登录”选项卡上,选中“允许与桌面交互的服务”复选框.记住要作为本地系统运行IIS管理服务.> d.停止并重新启动IIS管理服务.

Bean后置处理器 - InstantiationAwareBeanPostProcessor#applyBeanPostProcessorsBeforeInstantiation

Bean后置处理器 - InstantiationAwareBeanPostProcessor#applyBeanPostProcessorsBeforeInstantiation

在 createBean 方法中, doCreateBean 方法前, 调用了这样一句代码:

org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#createBean

// Give BeanPostProcessors a chance to return a proxy instead of the target bean instance.
// 在 bean实例化之前 应用后置处理,如果后置处理返回的bean不为空,则直接返回
// 返回的是一个寡妇对象, 属性什么的, spring不会去维护
// spring不推荐开发人员使用这个接口 InstantiationAwareBeanPostProcessor
Object bean = resolveBeforeInstantiation(beanName, mbdToUse);
if (bean != null) {
    return bean;
}

这是一个扩展方法, 如果返回了 bean , 那么就不走后面的创建流程了. 

要注意这里的执行时机: 对象实例化之前执行

@Nullable
protected Object resolveBeforeInstantiation(String beanName, RootBeanDefinition mbd) {
    Object bean = null;
    if (!Boolean.FALSE.equals(mbd.beforeInstantiationResolved)) {
        // Make sure bean class is actually resolved at this point.
        if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
            Class<?> targetType = determineTargetType(beanName, mbd);
            if (targetType != null) {
                bean = applyBeanPostProcessorsBeforeInstantiation(targetType, beanName);
                if (bean != null) {
                    bean = applyBeanPostProcessorsAfterInitialization(bean, beanName);
                }
            }
        }
        mbd.beforeInstantiationResolved = (bean != null);
    }
    return bean;
}

InstantiationAwareBeanPostProcessor 这个后置处理器, 不但定义了 实例化之前的处理器, 还定义了  实例化之后的处理器. 

所以, 这里如果返回的bean不为null, 还需要执行实例化之后的处理器, 来保证流程的完整性. 

在这里, 就不看实例化之后的回调了.

 

直接看 applyBeanPostProcessorsBeforeInstantiation() 方法:

@Nullable
protected Object applyBeanPostProcessorsBeforeInstantiation(Class<?> beanClass, String beanName) {
    for (BeanPostProcessor bp : getBeanPostProcessors()) {
        if (bp instanceof InstantiationAwareBeanPostProcessor) {
            InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
            Object result = ibp.postProcessBeforeInstantiation(beanClass, beanName);
            if (result != null) {
                return result;
            }
        }
    }
    return null;
}

 

首先来看一下, 这个接口:

public interface InstantiationAwareBeanPostProcessor extends BeanPostProcessor {

    @Nullable
    default Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
        return null;
    }

 
    default boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
        return true;
    }

   
    @Nullable
    default PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName)
            throws BeansException {

        return null;
    }

  
    @Deprecated
    @Nullable
    default PropertyValues postProcessPropertyValues(
            PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException {

        return pvs;
    }

}

注意到, 这个接口是 继承了 BeanPostProcessor , 表示他是一个 bean 的后置处理器. 和前面章节出现过的 BeanFactory 后置处理器 是不同的

 

通过断点调试, 不难发现, 6个后置处理器中, 满足条件的就三个, 按照执行顺序排列为: 

1.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor

2.CommonAnnotationBeanPostProcessor

3.AutowiredAnnotationBeanPostProcessor

 

那接下来, 就需要看一下, 里面分别干了什么

ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor

org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter#postProcessBeforeInstantiation

@Override
@Nullable
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
    return null;
}

从这里看, 他本身都没有实现或重写这个方法, 而是由它的父类去实现的.

里面啥也没干, 直接返回了 null.

 

CommonAnnotationBeanPostProcessor

@Override
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) {
    return null;
}

啥也没干

 

AutowiredAnnotationBeanPostProcessor

org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter#postProcessBeforeInstantiation

@Override
@Nullable
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
    return null;
}

他本身也没有实现这个方法, 而是父类去实现的.

里面也是啥也没干.

 

到这里, 这个后置处理器就执行完了, 大家都没干事情, 都是直接返回的 null. 所以, spring后续流程还是要走的

 

Bean后置处理器 - InstantiationAwareBeanPostProcessor#postProcessProperties

Bean后置处理器 - InstantiationAwareBeanPostProcessor#postProcessProperties

代码片段:

org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#populateBean

boolean hasInstAwareBpps = hasInstantiationAwareBeanPostProcessors();
//深度引用检查, 引用再引用
boolean needsDepCheck = (mbd.getDependencyCheck() != AbstractBeanDefinition.DEPENDENCY_CHECK_NONE);

PropertyDescriptor[] filteredPds = null;
if (hasInstAwareBpps) {
    if (pvs == null) {
        pvs = mbd.getPropertyValues();
    }
    for (BeanPostProcessor bp : getBeanPostProcessors()) {
        if (bp instanceof InstantiationAwareBeanPostProcessor) {
            InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
            //这里也是调用的实例化后的后置处理器, 只是调用的方法不一样
       //这里会进行 @Autowired 和 @Resource 的注入工作
PropertyValues pvsToUse = ibp.postProcessProperties(pvs, bw.getWrappedInstance(), beanName); if (pvsToUse == null) { if (filteredPds == null) { filteredPds = filterPropertyDescriptorsForDependencyCheck(bw, mbd.allowCaching); } pvsToUse = ibp.postProcessPropertyValues(pvs, filteredPds, bw.getWrappedInstance(), beanName); if (pvsToUse == null) { return; } } pvs = pvsToUse; } } }

同样的, 通过调试的方式, 来确定这里使用了那些后置处理器:

1.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor

2.CommonAnnotationBeanPostProcessor

3.AutowiredAnnotationBeanPostProcessor

 

ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor

@Override
public PropertyValues postProcessProperties(@Nullable PropertyValues pvs, Object bean, String beanName) {
    // Inject the BeanFactory before AutowiredAnnotationBeanPostProcessor''s
    // postProcessProperties method attempts to autowire other configuration beans.
    if (bean instanceof EnhancedConfiguration) {
        ((EnhancedConfiguration) bean).setBeanFactory(this.beanFactory);
    }
    return pvs;
}

这里是对 EnhancedConfiguration 提供支持. 其实他继承了 BeanFactoryAware 接口, 并且什么都没干

public interface EnhancedConfiguration extends BeanFactoryAware {}

EnhancedConfiguration 是 ConfigurationClassEnhancer 的一个内部接口, 是给 spring 自己内部使用的, 开发人员用不了这个, 也没必要

 

CommonAnnotationBeanPostProcessor

@Override
public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) {
    //找出类中标注了@Resource注解的属性和方法
    //else if (field.isAnnotationPresent(Resource.class))
    InjectionMetadata metadata = findResourceMetadata(beanName, bean.getClass(), pvs);
    try {
        metadata.inject(bean, beanName, pvs);
    }
    catch (Throwable ex) {
        throw new BeanCreationException(beanName, "Injection of resource dependencies failed", ex);
    }
    return pvs;
}

这里主要是对 @Resource 进行注入

findResourceMetadata在MergedBeanDefinitionPostProcessor#postProcessMergedBeanDefinition的时候执行过.

所以这里, 大概率是从缓存中拿取结果, 然后进行注入操作. 事实上, 也确实如此.

 

inject 最终会调用

org.springframework.beans.factory.annotation.InjectionMetadata.InjectedElement#inject 方法

protected void inject(Object target, @Nullable String requestingBeanName, @Nullable PropertyValues pvs)
        throws Throwable {

    if (this.isField) {
        Field field = (Field) this.member;
        ReflectionUtils.makeAccessible(field);
        field.set(target, getResourceToInject(target, requestingBeanName));
    }
    else {
        if (checkPropertySkipping(pvs)) {
            return;
        }
        try {
            Method method = (Method) this.member;
            ReflectionUtils.makeAccessible(method);
            method.invoke(target, getResourceToInject(target, requestingBeanName));
        }
        catch (InvocationTargetException ex) {
            throw ex.getTargetException();
        }
    }
}

这里分两种情况进行处理, 一种是 通过字段注入, 另一种是通过方法注入.

字段注入的方式是比较多见的. 

getResourceToInject() 方法是一个被重写了的方法:

org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.ResourceElement#getResourceToInject

他是在一个内部类中被重写的方法.

@Override
protected Object getResourceToInject(Object target, @Nullable String requestingBeanName) {
    return (this.lazyLookup ? buildLazyResourceProxy(this, requestingBeanName) :
            getResource(this, requestingBeanName));
}

默认情况下, lazy = false, 所以会走 getResource() 方法

org.springframework.context.annotation.CommonAnnotationBeanPostProcessor#getResource

protected Object getResource(LookupElement element, @Nullable String requestingBeanName)
        throws NoSuchBeanDefinitionException {

    if (StringUtils.hasLength(element.mappedName)) {
        return this.jndiFactory.getBean(element.mappedName, element.lookupType);
    }
    if (this.alwaysUseJndiLookup) {
        return this.jndiFactory.getBean(element.name, element.lookupType);
    }
    if (this.resourceFactory == null) {
        throw new NoSuchBeanDefinitionException(element.lookupType,
                "No resource factory configured - specify the ''resourceFactory'' property");
    }
    return autowireResource(this.resourceFactory, element, requestingBeanName);
}

根据设置的不同属性, 进不同的方法, 这里主要看 autowireResource , 因为一般情况是不需要去设置这些属性的

protected Object autowireResource(BeanFactory factory, LookupElement element, @Nullable String requestingBeanName)
        throws NoSuchBeanDefinitionException {

    Object resource;
    Set<String> autowiredBeanNames;
    String name = element.name;

    if (factory instanceof AutowireCapableBeanFactory) {
        AutowireCapableBeanFactory beanFactory = (AutowireCapableBeanFactory) factory;
        DependencyDescriptor descriptor = element.getDependencyDescriptor();
        //优先使用 根据 name 注入, 这个 name 就是 beanName
        //当 beanName 找不到时, 才就会去根据 type 来注入
        //依据是 !factory.containsBean(name) 
        if (this.fallbackToDefaultTypeMatch && element.isDefaultName && !factory.containsBean(name)) {
            autowiredBeanNames = new LinkedHashSet<>();
            //根据type(类型)来进行依赖注入
            resource = beanFactory.resolveDependency(descriptor, requestingBeanName, autowiredBeanNames, null);
            if (resource == null) {
                //如果返回时 resource , 则表明, spring容器中找不到, 也创建不了 这个依赖的bean
                throw new NoSuchBeanDefinitionException(element.getLookupType(), "No resolvable resource object");
            }
        }
        else {
            //根据name(名称)来进行依赖注入
            resource = beanFactory.resolveBeanByName(name, descriptor);
            autowiredBeanNames = Collections.singleton(name);
        }
    }
    else {
        resource = factory.getBean(name, element.lookupType);
        autowiredBeanNames = Collections.singleton(name);
    }

    if (factory instanceof ConfigurableBeanFactory) {
        ConfigurableBeanFactory beanFactory = (ConfigurableBeanFactory) factory;
        for (String autowiredBeanName : autowiredBeanNames) {
            if (requestingBeanName != null && beanFactory.containsBean(autowiredBeanName)) {
                beanFactory.registerDependentBean(autowiredBeanName, requestingBeanName);
            }
        }
    }

    return resource;
}

都说, @Resource 在不设置 name 和 type 的情况下, 是优先使用 name 去 获取/创建 依赖的 bean , 如果name找不到, 才会使用 type.

这段代码, 就是支撑的依据.

 

AutowiredAnnotationBeanPostProcessor

@Override
public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) {
    InjectionMetadata metadata = findAutowiringMetadata(beanName, bean.getClass(), pvs);
    try {
        metadata.inject(bean, beanName, pvs);
    }
    catch (BeanCreationException ex) {
        throw ex;
    }
    catch (Throwable ex) {
        throw new BeanCreationException(beanName, "Injection of autowired dependencies failed", ex);
    }
    return pvs;
}

这里主要是对 @Autowired 和 @Value 进行注入的. findAutowiringMetadata

findAutowiringMetadata在MergedBeanDefinitionPostProcessor#postProcessMergedBeanDefinition的时候执行过.

所以此处, 是从缓存中拿取结果, 然后进行注入操作.

最终会调用本类中的方法:

org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.AutowiredFieldElement#inject

@Override
protected void inject(Object bean, @Nullable String beanName, @Nullable PropertyValues pvs) throws Throwable {
    Field field = (Field) this.member;
    Object value;
    if (this.cached) {
        value = resolvedCachedArgument(beanName, this.cachedFieldValue);
    }
    else {
        DependencyDescriptor desc = new DependencyDescriptor(field, this.required);
        desc.setContainingClass(bean.getClass());
        Set<String> autowiredBeanNames = new LinkedHashSet<>(1);
        Assert.state(beanFactory != null, "No BeanFactory available");
        TypeConverter typeConverter = beanFactory.getTypeConverter();
        try {
            //根据 type 进行依赖注入
            value = beanFactory.resolveDependency(desc, beanName, autowiredBeanNames, typeConverter);
        }
        catch (BeansException ex) {
            throw new UnsatisfiedDependencyException(null, beanName, new InjectionPoint(field), ex);
        }
        synchronized (this) {
            if (!this.cached) {
                if (value != null || this.required) {
                    this.cachedFieldValue = desc;
                    registerDependentBeans(beanName, autowiredBeanNames);
                    if (autowiredBeanNames.size() == 1) {
                        String autowiredBeanName = autowiredBeanNames.iterator().next();
                        if (beanFactory.containsBean(autowiredBeanName) &&
                                beanFactory.isTypeMatch(autowiredBeanName, field.getType())) {
                            this.cachedFieldValue = new ShortcutDependencyDescriptor(
                                    desc, autowiredBeanName, field.getType());
                        }
                    }
                }
                else {
                    this.cachedFieldValue = null;
                }
                this.cached = true;
            }
        }
    }
    if (value != null) {
        ReflectionUtils.makeAccessible(field);
        field.set(bean, value);
    }
}

 

这里有一个隐藏的后置处理器: SmartInstantiationAwareBeanPostProcessor

留到下一篇看

今天的关于c# – 在System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo)中拒绝访问的分享已经结束,谢谢您的关注,如果想了解更多关于.net – ProcessStartInfo的UseShellExecute和CreateNoWindow有什么区别?、asp.net – System.Diagnostics.Process.Start不能从IIS工作、Bean后置处理器 - InstantiationAwareBeanPostProcessor#applyBeanPostProcessorsBeforeInstantiation、Bean后置处理器 - InstantiationAwareBeanPostProcessor#postProcessProperties的相关知识,请在本站进行查询。

本文标签: