1

我可以自己通过winforms访问Firestore,并且可以正常工作。但是,如果我尝试通过外部命令访问 Firestore,则会收到错误消息:

System.IO.FileNotFoundException:'无法加载文件或程序集'Google.Apis.Auth,Version=1.44.1.0,Culture=neutral,PublicKeyToken=XXXXXXXXXX'或其依赖项之一。该系统找不到指定的文件。'

奇怪的部分是我有Google.Apis.Auth, Version=1.46.0.0作为参考。

如果我将参考版本更改为1.44.1.0,那么它会要求我将参考版本更改为1.21.0.0

我怎样才能使这项工作?

这是我的代码:

应用程序.cs

#region Namespaces
using System;
using System.Collections.Generic;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using System.Reflection;
using System.Windows.Media.Imaging;
using Google.Cloud.Firestore;
using System.Windows.Forms;
#endregion

namespace testformsFirestore
{
    class App : IExternalApplication
    {
        public Result OnStartup(UIControlledApplication a)
        {

            RibbonPanel panel = ribbonPanel(a);
            string thisAssemblyPath = Assembly.GetExecutingAssembly().Location;
            PushButton button = panel.AddItem(new PushButtonData("testformsFirestore" ,
                "testformsFirestore Button", thisAssemblyPath, "testformsFirestore.Command")) as PushButton; 

            button.ToolTip = "this is a simple tooltip";
            var globePath = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "Corvette.PNG");
            
            Uri uriImage = new Uri(globePath);
            BitmapImage largeImage = new BitmapImage(uriImage);
            button.LargeImage = largeImage;

            

            a.ApplicationClosing += a_ApplicationClosing;

            // Set application idling
            a.Idling += a_Idling;


            return Result.Succeeded;
        }

        void a_Idling(object sender, Autodesk.Revit.UI.Events.IdlingEventArgs e)
        {

        }

        void a_ApplicationClosing(object sender, Autodesk.Revit.UI.Events.ApplicationClosingEventArgs e)
        {
            throw new NotImplementedException();
        }


        public RibbonPanel ribbonPanel(UIControlledApplication a)
        {
            string tab = "My test tab";
            RibbonPanel ribbonPanel = null;
            try
            {
                a.CreateRibbonTab(tab);
            }
            catch { }
            try
            {
                RibbonPanel panel = a.CreateRibbonPanel(tab, "test");
            }
            catch { }

            List<RibbonPanel> panels = a.GetRibbonPanels(tab);
            foreach (RibbonPanel p in panels)
            {
                if (p.Name == "test")
                {
                    ribbonPanel = p;
                }
            }
            return ribbonPanel;
        }


        public Result OnShutdown(UIControlledApplication a)
        {
            return Result.Succeeded;
        }
    }
}

命令.cs

#region Namespaces
using System;
using System.Collections.Generic;
using System.Diagnostics;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
#endregion

namespace testformsFirestore
{
    [Transaction(TransactionMode.Manual)]
    public class Command : IExternalCommand
    {
        public Result Execute(
          ExternalCommandData commandData,
          ref string message,
          ElementSet elements)
        {
            UIApplication uiapp = commandData.Application;
            UIDocument uidoc = uiapp.ActiveUIDocument;
            Application app = uiapp.Application;
            Document doc = uidoc.Document;

            Form1 form1 = new Form1();


            using (Transaction tx = new Transaction(doc, "Transaction Name"))
            {
                try
                {
                    tx.Start();

                    // Code goes here.
                    form1.Show();


                    tx.Commit();
                }
                catch (Exception e)
                {
                    Debug.Print(e.Message);
                    tx.RollBack();
                }
            }

            return Result.Succeeded;
        }
    }
}

Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Google.Cloud.Firestore;

namespace testformsFirestore
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            string path = "\\\\Mac\\Home\\Desktop\\testformsFirestore\\testFormsFirestore\\bin\\Debug\\myProjectJsonPath.json";
            Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", path);
            
            FirestoreDb db = FirestoreDb.Create("myProjectId");

            CollectionReference collection = db.Collection("Users");

            getSnapshot();
            async void getSnapshot()
            {
                // A CollectionReference is a Query, so we can just fetch everything
                QuerySnapshot allUsers = await collection.GetSnapshotAsync();
                foreach (DocumentSnapshot document in allUsers.Documents)
                {

                    MessageBox.Show(document.GetValue<string>("username").ToString()); ;
                    
                }

            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

应用程序配置

        <assemblyIdentity name="Google.Apis.Auth" publicKeyToken="XXXXXXXXXX" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-1.46.0.0" newVersion="1.46.0.0" />
      </dependentAssembly>

堆栈跟踪

System.IO.FileNotFoundException
  HResult=0x80070002
  Message=Could not load file or assembly 'Google.Apis.Auth, Version=1.21.0.0, Culture=neutral, PublicKeyToken=XXXXXXXXXX' or one of its dependencies. The system cannot find the file specified.
  Source=Google.Api.Gax.Grpc
  StackTrace:
   at Google.Api.Gax.Grpc.ChannelPool.<CreateChannelCredentialsUncached>d__5.MoveNext()
   at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1.Start[TStateMachine](TStateMachine& stateMachine)
   at Google.Api.Gax.Grpc.ChannelPool.CreateChannelCredentialsUncached()
   at System.Threading.Tasks.Task`1.InnerInvoke()
   at System.Threading.Tasks.Task.Execute()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Google.Api.Gax.TaskExtensions.WaitWithUnwrappedExceptions(Task task)
   at Google.Api.Gax.Grpc.ChannelPool.GetChannel(GrpcAdapter grpcAdapter, String endpoint, GrpcChannelOptions channelOptions)
   at Google.Api.Gax.Grpc.ClientBuilderBase`1.CreateCallInvoker()
   at Google.Cloud.Firestore.V1.FirestoreClientBuilder.BuildImpl()
   at Google.Cloud.Firestore.FirestoreDbBuilder.Build()
   at DockableDialog.RegisterDockableWindow.Execute(ExternalCommandData commandData, String& message, ElementSet elements) in \\Mac\Home\Desktop\Revit-API---C-\DockableWPFPane2021\DockablePane2021\Ribbon.cs:line 137
   at apiManagedExecuteCommand(AString* assemblyName, AString* className, AString* vendorDescription, MFCApp* pMFCApp, DBView* pDBView, AString* message, Set<ElementId\,std::less<ElementId>\,tnallc<ElementId> >* ids, Map<AString\,AString\,std::less<AString>\,tnallc<std::pair<AString const \,AString> > >* data, AString* exceptionName, AString* exceptionMessage)

日志

'Revit.exe' (CLR v4.0.30319: DefaultDomain): Loaded 'C:\Users\myName\AppData\Roaming\Autodesk\Revit\Addins\2021\testForms.dll'. 
System.Windows.Media.Animation Warning: 6 : Unable to perform action because the specified Storyboard was never applied to this object for interactive control.; Action='Stop'; Storyboard='System.Windows.Media.Animation.Storyboard'; Storyboard.HashCode='6995273'; Storyboard.Type='System.Windows.Media.Animation.Storyboard'; TargetElement='Autodesk.Internal.Windows.ToolTip'; TargetElement.HashCode='54242802'; TargetElement.Type='Autodesk.Internal.Windows.ToolTip'
'Revit.exe' (CLR v4.0.30319: DefaultDomain): Loaded 'C:\Users\myName\AppData\Roaming\Autodesk\Revit\Addins\2021\Google.Cloud.Firestore.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'Revit.exe' (CLR v4.0.30319: DefaultDomain): Loaded 'C:\Users\myName\AppData\Roaming\Autodesk\Revit\Addins\2021\Google.Cloud.Firestore.V1.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'Revit.exe' (CLR v4.0.30319: DefaultDomain): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.ValueTuple\v4.0_4.0.0.0__cc7b13ffcd2ddd51\System.ValueTuple.dll'. Module was built without symbols.
'Revit.exe' (CLR v4.0.30319: DefaultDomain): Loaded 'C:\Users\myName\AppData\Roaming\Autodesk\Revit\Addins\2021\Google.Api.Gax.Grpc.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'Revit.exe' (CLR v4.0.30319: DefaultDomain): Loaded 'C:\Users\myName\AppData\Roaming\Autodesk\Revit\Addins\2021\Google.Api.Gax.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'Revit.exe' (CLR v4.0.30319: DefaultDomain): Loaded 'C:\Users\myName\AppData\Roaming\Autodesk\Revit\Addins\2021\Grpc.Core.Api.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'Revit.exe' (CLR v4.0.30319: DefaultDomain): Loaded 'C:\Users\myName\AppData\Roaming\Autodesk\Revit\Addins\2021\Google.Api.Gax.Grpc.GrpcCore.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'Revit.exe' (CLR v4.0.30319: DefaultDomain): Loaded 'C:\Users\myName\AppData\Roaming\Autodesk\Revit\Addins\2021\Google.Apis.Auth.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'Revit.exe' (CLR v4.0.30319: DefaultDomain): Loaded 'C:\Users\myName\AppData\Roaming\Autodesk\Revit\Addins\2021\Google.Apis.Core.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'Revit.exe' (CLR v4.0.30319: DefaultDomain): Loaded 'C:\Users\myName\AppData\Roaming\Autodesk\Revit\Addins\2021\Grpc.Auth.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
An unhandled exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll
Could not load file or assembly 'Google.Apis.Auth, Version=1.21.0.0, Culture=neutral, PublicKeyToken=XXXXXXXXXX' or one of its dependencies. The system cannot find the file specified.
4

1 回答 1

0

请注意,您为身份验证设置了环境变量,并FirestoreDb db = FirestoreDb.Create("myProjectId");在单击按钮时在方法中创建了 Firestore 客户端 ( )。

        private void button1_Click(object sender, EventArgs e)
        {

            string path = "\\\\Mac\\Home\\Desktop\\testformsFirestore\\testFormsFirestore\\bin\\Debug\\myProjectJsonPath.json";
            Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", path);
            
            FirestoreDb db = FirestoreDb.Create("myProjectId");

            CollectionReference collection = db.Collection("Users");

设置环境变量:

string path = "\\\\Mac\\Home\\Desktop\\testformsFirestore\\testFormsFirestore\\bin\\Debug\\myProjectJsonPath.json";
Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", path)

并进行客户端的初始化:

FirestoreDb db = FirestoreDb.Create("myProjectId");
CollectionReference collection = db.Collection("Users");

在您的应用程序的上一步中。

于 2020-06-29T15:17:41.637 回答